2012-02-09 32 views
3

如何在asp.net C#应用程序中创建子域?我正在使用一个asp.net门户。如何使用ASP.NET创建动态子域?

我有我的网站,它将所有* .domain.com调用重定向到domain.com。我想达到的目标是,首先当用户输入一个动态子域名时,他应该被引导到它的主页,就像用户写www.microsite1.domain.com一样,那么该网站应该指向页面〜/ Microsite/Home.aspx ?value = microsite1,当用户访问www.microsite1.domain.com/about.aspx时,我应该能够获得参数value1 = about。

回答

4

第一步是将子域名置于DNS主机服务器中。要做到这一点,你需要操纵dns文件。例如,如果你使用BIND作为DNS服务器,你可以打开保存你的DNS配置的文本文件,例如:“c:\ program files \ dns \ var \ mysite.com”,然后你添加一行为

subdomain.mysite.com. IN A 111.222.333.444 

此外,您还可以更改文件的ID以向BIND发送消息以更新子域。

第二步是将新的子域重定向到正确的目录。您可以使用rewritepath

做的 protected void Application_BeginRequest(Object sender, EventArgs e)Global.asax
protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    if (HttpContext.Current.Request.Url.Host.StartsWith("subdomain.")) 
    { 
     // here you need to find where to redirect him by reading the url 
     // and find the correct file. 
     HttpContext.Current.RewritePath("/subdomain/" + Request.Path, false); 
    } 


    // .... rest code 
} 

它不是那么容易,不是那么难......也许还有一些更小的问题,如写入权限DNS。你也需要知道DNS,阅读手册。