Click here to Skip to main content
14,932,457 members
Please Sign up or sign in to vote.
4.43/5 (3 votes)
See more:
I've URLs in my website and which are using query string values For example:

http://foo.com/xyzPage.aspx?barvalue=yehaa
to
http://yehaa.foo.com/

Please suggest how it can be accomplished without actually creating subdomains on server ..

I've IIS 7.5 installed on server machine and using Asp.net 4.0.
Posted
Updated 2-Apr-16 2:03am
v3

1 solution

C#
protected void Application_BeginRequest(Object sender, EventArgs e)
{   
    var SubDomain = GetSubDomain(HttpContext.Current.Request.Host);
    // this is a simple example, you can place your variables base on subdomain
    if(!String.IsNullOrEmpty(SubDomain))
       RewritePath(HttpContext.Current.Request.Path + SubDomain + "/", false);  
}

// from : http://madskristensen.net/post/Retrieve-the-subdomain-from-a-URL-in-C.aspx
private static string GetSubDomain(Uri url)
{
  string host = url.Host;
  if (host.Split('.').Length > 1)
  {
    int index = host.IndexOf(".");
    return host.Substring(0, index);
  }

  return null;
}
   

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