【问题标题】:Creating subdomains using C#使用 C# 创建子域
【发布时间】:2018-06-13 11:15:50
【问题描述】:

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

username.example.com 

或者

  username2.example.com

我熟悉静态/动态路由。我猜这可以通过其中之一或虚拟目录来完成?

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

【问题讨论】:

  • 请注意,您还必须在 DNS 端处理此问题,或将任何不匹配的 A 记录请求定向到您的服务器。
  • @john 是的,我想是的......我只需要有人给出一些好的答案,以便为每个想了解更多关于这个主题的人澄清一下......
  • 我对这方面的 IIS 了解不多,无法给出答案,但只是想确保您不会忘记 :)
  • @john thx for that =) 我想知道是否有人会尽快回复
  • 有人吗? =)

标签: c# asp.net asp.net-mvc routing subdomain


【解决方案1】:

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

第二步,假设您将 BIND 用于 dns,因此您打开正确的 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        
}

【讨论】:

  • 好吧,我去看看 =)
  • @User987 我已经实现了那个场景,但是我没有一些具体的源代码可以展示给你,因为它是一个复杂的,包括数据库读取和设置。但这是大体思路,基本步骤。
  • 你至少可以发布 url 重写部分的源代码吗? =) 我会找到一种方法进行数据库读/写设置
  • @User987 所有困难的代码是分析Request.Path 并提取“用户名”......然后你只需调用这个HttpContext.Current.RewritePath("example.com/users.aspx?userid=12345", false);
  • @User987 我已经复制/粘贴了我拥有的基本代码
猜你喜欢
  • 2011-10-28
  • 2010-11-29
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 2015-04-24
  • 1970-01-01
  • 2012-01-01
  • 2014-11-12
相关资源
最近更新 更多