简体   繁体   中英

Create Dynamic subdomain in asp.net

I need to do URL rewriting in such a way that

if my request is abc.domain.com, then request should be processed such as

domain.com/default.aspx?cid=abc

for example, if i give .domain.com, request should be assumed as domain.com/default.aspx?cid=

it is not mandatory that i need to give abc alone the subdomain can be any.. but my request should be processed assuming it as querystring value.

My domain is in Shared hosting...

can anyone throw light in this..

The Subdomain must be created and configure on DNS server and on IIS.

After you have setup your site to accept that subdomains, you may direct use the RewritePath to map a subdomain to a specific file with different parameters.

Starting from Application_BeginRequest you check and find the subdomain and rewrite the path as:

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

Similar:
How to remap all the request to a specific domain to subdirectory in ASP.NET
Redirect Web Page Requests to a Default Sub Folder
Retrieve the subdomain from a URL in C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM