简体   繁体   English

如何使用ASP.NET创建动态子域?

[英]How to Create Dynamic Sub-domain with ASP.NET?

How can I create a subdomain in an asp.net C# application? 如何在asp.net C#应用程序中创建子域? I am working with an asp.net portal. 我正在使用asp.net门户。

I have my site which redirects all *.domain.com calls to domain.com. 我的网站将所有* .domain.com电话重定向到domain.com。 What I want to acheive is that first when the user enters a dynamic subdomain name he should be directed to its home page like if user writes www.microsite1.domain.com, then the site should point to page ~/Microsite/Home.aspx?value=microsite1, and when the user accesses www.microsite1.domain.com/about.aspx then i should able to get the argument value1=about. 我想要实现的是,首先当用户输入动态子域名时,应将其定向到其主页,就像用户写入www.microsite1.domain.com一样,那么该站点应该指向页面〜/ Microsite / Home.aspx ?value = microsite1,当用户访问www.microsite1.domain.com/about.aspx时,我应该能够得到参数value1 = about。

The first step is to place the subdomain name in the DNS Host Server. 第一步是将子域名放在DNS主机服务器中。 To do that you need to manipulate the dns files. 为此,您需要操作dns文件。 For example if you use BIND as DNS server you go and open the text file that keep your DNS configuration eg: "c:\\program files\\dns\\var\\mysite.com" and there you add a line as 例如,如果您使用BIND作为DNS服务器,您可以打开保存DNS配置的文本文件,例如:“c:\\ program files \\ dns \\ var \\ mysite.com”,然后在那里添加一行

subdomain.mysite.com.   IN  A   111.222.333.444

Also you change the ID of the file to give a message to BIND to update the subdomains. 您还可以更改文件的ID以向BIND发送消息以更新子域。

Second step is to redirect the new subdomain to the correct directory. 第二步是将新子域重定向到正确的目录。 You do that on the protected void Application_BeginRequest(Object sender, EventArgs e) on Global.asax using rewritepath 您可以使用rewritepath在Global.asax上的protected void Application_BeginRequest(Object sender, EventArgs e)上执行此操作

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   
}

Its not so easy, not so hard... maybe there are some more minor issues like permissions to write to dns. 它不是那么容易,也不是那么难......也许还有一些小问题,比如写入dns的权限。 Also you need to know dns, read the manual about. 您还需要了解dns,阅读手册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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