使用 C# 创建子域

Creating subdomains using C#

我有一个关于使用 C# 创建子域的问题。所以这背后的想法是用户可以在我们的网络应用程序上创建一个子域,如下所示:

username.example.com 

  username2.example.com

我熟悉 static/dynamic 条路线。我猜这可以通过其中之一或虚拟目录来完成?

如何在 C# 和 .NET 中执行此操作?

首先假设您的站点有一个专用 IP,以避免绑定到 IIS

第二步,假设您将 BIND 用于 dns,因此您打开正确的 txt 文件并在其中添加或删除正确的子域并更新时间戳,然后使用命令重新启动 BIND 服务从 asp.net 调用...(您需要了解 DNS 的一些基本知识,并授予对池的访问权限才能 read/write 那里)

然后您可以在 global.asax Application_BeginRequest 的第一个电话上阅读

HttpContext.Current.Request.Path

也包含子域,并使用

翻译它
HttpContext.Current.RewritePath

类似于

username.example.com  -> example.com/users.aspx?userid=123456

这是总体思路(这是在 global.asax 上)...

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    string sThePathToReWrite = sFindTheRealPath();

    if (sThePathToReWrite != null){
        HttpContext.Current.RewritePath(sThePathToReWrite, false);
    }       
}

string sFindTheRealPath()
{
    string strCurrentPath = HttpContext.Current.Request.Path;   

    // analyze the strCurrentPath 
    // find for example the userid from the url
    // and return the translate one - if not find anything return null
    return "example.com/users.aspx?userid=" + userid        
}