Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:
  • 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!


This is one potential starting point:
C#
private string PrefixSubdomain(string subdomain)
{
    var builder = new System.UriBuilder(Request.Url.ToString());
    // ...
}

VB.NET
Private Function PrefixSubdomain(subdomain As String) As String
	Dim builder As New System.UriBuilder(Request.Url.ToString())
	' ...
End Function

You needn't use UriBuilder! You could instead, for example, use a regular expression (though that may be less elegant by my standards).
Posted
Updated 13-Feb-19 18:15pm
v3

C#
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.
 
Share this answer
 
v14
Comments
AspDotNetDev 17-Mar-11 15:17pm    
Revise away! Though, I think I have you beat. I'm about to post the solution I just came up with. I'll test both yours and mine out after I post mine.
#realJSOP 17-Mar-11 15:19pm    
I just posted my final edit.
#realJSOP 17-Mar-11 15:24pm    
when you add a subdomain to "www.blah.com", do you strip off the "www" and replace it withthe new subdomain?

Given all the gyrations you have to perform to do it a different way, you have to question the value of avoiding using string.Replace. It's a hollow victory.
AspDotNetDev 17-Mar-11 15:26pm    
For my purposes, I will never deal with "www.site.com", but dealing with that could be of value to somebody else. Also, keep in mind that "http" could be "https" and if this function were not making use of "Request.Url" it might also have to deal with the case of "http://" being missing entirely. That is why my solution makes use of UriBuilder... because it's a class that deals with these nuances automatically.
#realJSOP 17-Mar-11 16:00pm    
My solution doesn't depend on http/https. It merely splits the strings on forward slashes, and then rebuilds the url a piece at a time.
C#
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.
 
Share this answer
 
v3
Comments
#realJSOP 18-Mar-11 7:31am    
Geeze. I'm the only one that answered?
AspDotNetDev 18-Mar-11 12:38pm    
Well, you and me. Guess we were the only two up to the challenge.
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;
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900