2013-02-21 115 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#