Click here to Skip to main content
14,173,165 members
Rate this:
 
Please Sign up or sign in to vote.
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:
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

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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
   
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.
AspDotNetDev 17-Mar-11 16:40pm
   
Yep, didn't mean to imply that. I was just mentioning that because it could cause problems if one were to use string.Replace().
AspDotNetDev 17-Mar-11 16:38pm
   
This does not seem to work for me. When I try running it locally, the output is "localhost". When I run it on a live site, the output is "blog.site.com". It seems to omit the protocol, the port, the path, and the page. Also, I checked my solution and it seems to work fine (had to make a small change).
#realJSOP 17-Mar-11 17:33pm
   
It's probably because I used Host instead of the entire URL. I tested this in a console app (I was more concerned with the parsing/reassembling).
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
   
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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;
   
v3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190524.3 | Last Updated 14 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100