Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET .NET VB.NET
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 17-Mar-11 8:56am
AspDotNetDev191.1K
Edited 17-Mar-11 9:08am
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.
  Permalink  
v14
Comments
AspDotNetDev at 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.
John Simmons / outlaw programmer at 17-Mar-11 15:19pm
   
I just posted my final edit.
John Simmons / outlaw programmer at 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 at 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.
John Simmons / outlaw programmer at 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 at 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 at 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).
John Simmons / outlaw programmer at 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.
  Permalink  
v3
Comments
   
Geeze. I'm the only one that answered?
AspDotNetDev at 18-Mar-11 12:38pm
   
Well, you and me. Guess we were the only two up to the challenge.

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

  Print Answers RSS
0 OriginalGriff 6,649
1 Sergey Alexandrovich Kryukov 6,280
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 18 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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