c# - 使用 C# 创建子域

标签 c# asp.net asp.net-mvc routes subdomain

我有一个关于使用 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/

相关文章:

javascript - Jquery/Javascript : How can I get ClientID for dynamically generated controls

asp.net-mvc - ASP.NET MVC 下拉列表未选择渲染值

c# - 防止在 ASP.NET MVC 页面上重复提交/发布

c# - 每秒更新一次 SQL Server 数据库表

c# - 运行时 Windows 窗体 : Is it possible to insert . cs 文件?

asp.net - Web 应用程序中图像、视频和其他 Assets 的子域

c# - C#中合并嵌套If(C#中的短路关键字)

c# - "is not null here"

c# - 关闭发布版本的 MVC 编译调试

asp.net-mvc - asp.net MVC 验证框架的选项