我有一个关于使用 C# 创建子域的问题。因此,其背后的想法是用户可以在我们的 Web 应用程序上创建一个子域,如下所示:
username.example.com
或者
username2.example.com
我熟悉静态/动态路由。我猜这可以通过其中之一或虚拟目录来完成?
如何在 C# 和 .NET 中执行此操作?
最佳答案
首先,假设您的站点有一个专用 IP,以避免绑定(bind)到 IIS
第二步,假设您对 dns 使用 BIND,因此您打开正确的 txt 文件,并在其中添加或删除正确的子域并更新时间戳,然后使用以下命令调用重新启动 BIND 服务asp.net…(您需要了解一些 DNS 基础知识,并授予对池的访问权限才能在那里读/写)
然后您可以在 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
}
关于c# - 使用 C# 创建子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50835847/