2013-02-21 114 views
0

我需要做的URL重写以这样的方式创建asp.net动态子域

如果我的要求是abc.domain.com,然后请求应被处理,如

domain.com/的Default.aspx?CID = ABC

例如,如果我给.domain.com,请求应被假定为domain.com/default.aspx?cid=

它不是强制性的,我需要给abc单独的子域可以是任何..但我的请求应该被假定为查询字符串值。

我的域名是在共享托管...

任何人都可以在此扔光..

回答

0

必须创建子域和配置DNS服务器和IIS。

当您设置您的网站接受该子域名后,您可以直接使用RewritePath将子域名映射到具有不同参数的特定文件。

Application_BeginRequest开始您检查并找到子域和重写路径:

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

类似:
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#