挑战:优雅地将子域名前缀为URL [英] Challenge: Elegantly Prefix Subdomain To URL
问题描述
答案可以是C#或VB.NET。这是挑战。将 http://blog.site.com/path/page.aspx 转换为 http://mobile.blog.site.com/path /page.aspx 使用最优雅的方法。我自己可以这样做,但我想你们中的一个人可能会想出一些比我想象的更聪明的东西。
这里有一些规则:
- 避免硬编码字符串。 http,移动,博客,网站,路径和页面可能都会发生变化,因此您无法使用
string.Replace ()
。 - site.com将更改为mobile.site.com。 myapp.blog.site.com将更改为mobile.myapp.blog.site.com。
- 尝试使用现有的.Net类(例如,
System.UriBuilder
)而不是重新发明轮子。 - 如果您的解决方案在某种程度上比已发布的其他解决方案更好,也可以发布您的解决方案!
- Avoid hardcoding strings. "http", "mobile", "blog", "site", "path", and "page" may all change, so you can't use those with, say,
string.Replace()
. - "site.com" would change to "mobile.site.com". "myapp.blog.site.com" would change to "mobile.myapp.blog.site.com".
- Try to make use of existing .Net classes (e.g.,
System.UriBuilder
) rather than reinventing the wheel. - If your solution is better in some way than the other solutions already posted, post yours too!
The answer can be in C# or VB.NET. Here's the challenge. Transform http://blog.site.com/path/page.aspx into http://mobile.blog.site.com/path/page.aspx using the most elegant method possible. I can do this myself, but I think one of you may come up with something more clever than I have thought of so far.
Here are some rules:
private string PrefixSubdomain(string subdomain)
{
var builder = new System.UriBuilder(Request.Url.ToString());
// ...
}
Private Function PrefixSubdomain(subdomain As String) As String
Dim builder As New System.UriBuilder(Request.Url.ToString())
' ...
End Function
你不需要使用 UriBuilder
!例如,您可以使用正则表达式(虽然根据我的标准可能不那么优雅)。
You needn't use UriBuilder
! You could instead, for example, use a regular expression (though that may be less elegant by my standards).
推荐答案
private string PrefixSubDomain(string subDomain)
{
// split the string
string[] parts = Request.Url.ToString().Split('/');
// create a StringBuilder to economize overhead - init with
// the first element of the split string array
StringBuilder newPath = new StringBuilder(parts[0]);
// now process the remaining array elements
for (int i = 1; i < parts.Length; i++)
{
if (i == 2)
{
newPath.AppendFormat("/{0}", subDomain);
if (subDomain.EndsWith("."))
{
newPath.Append(parts[i]);
}
else
{
newPath.AppendFormat(".{0}", parts[i]);
}
}
else
{
newPath.AppendFormat("/{0}", (string.IsNullOrEmpty(parts[i]) ? "" : parts[i]));
}
}
return newPath.ToString();
}
对所有版本感到抱歉。当我想到的时候,我正在修理。
Sorry about all the revisions. I was fixing as I thought of stuff.
private string PrefixSubdomain(string subdomain)
{
var builder = new UriBuilder(Request.Url);
builder.Host = subdomain + "." + builder.Host;
return builder.Uri.ToString();
}
我真的不喜欢我使用字符串常量(。)。除此之外,这是我能想到的最优雅的代码。
I don't really like that I am using a string constant ("."). Other than that, this is the most elegant code I could come up with.
var subDomain = "donation";
var builder = new UriBuilder(Request.Url.AbsoluteUri);
var host = builder.Host;
string[] parts = host.Split('.');
if (parts.Length > 2)
{
//there is sub
parts[0] = subDomain;
}
//merge 'em
host ="";
for (int i = 0; i < parts.Length; i++)
{
host += parts[i];
}
builder.Host = host;
这篇关于挑战:优雅地将子域名前缀为URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!