【问题标题】:How to Create Dynamic Sub-domain with ASP.NET?如何使用 ASP.NET 创建动态子域?
【发布时间】:2012-02-09 07:14:51
【问题描述】:

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

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

【问题讨论】:

    标签: asp.net dynamic subdomain


    【解决方案1】:

    第一步是将子域名放入DNS Host Server。为此,您需要操作 dns 文件。例如,如果您使用 BIND 作为 DNS 服务器,您可以打开保存 DNS 配置的文本文件,例如:“c:\program files\dns\var\mysite.com”,然后在其中添加一行

    subdomain.mysite.com.   IN  A   111.222.333.444
    

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

    第二步是将新的子域重定向到正确的目录。你在 Global.asax 上的 protected void Application_BeginRequest(Object sender, EventArgs e) 上使用 rewritepath 执行此操作

    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,阅读手册了解一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      相关资源
      最近更新 更多