【问题标题】:how to create a single webapplication as a sub domain for many companies如何为许多公司创建单个 Web 应用程序作为子域
【发布时间】:2011-11-29 17:32:37
【问题描述】:

我正在开展一个项目,您可以使用您的公司名称订阅该项目,并且您可以使用网站的所有功能,专门用于您的公司。 例如,公司 abcd 可以从我们的网站获取自己的网址,例如

www.test.com/abcd/productlist.aspx

公司 efgh 也可以使用自己的 url 登录并查看其产品列表。

www.test.com/efgh/productlist.aspx

任何人都可以帮助我如何以最佳方法在我的网站上实现这一点

我正在考虑使用 Global.ascx 文件来区分公司的方法, 我将为每个有效请求编写代码从 global.ascx 中的 url 中提取公司名称,并且在所有页面中我将放置 this.form.action = request.rawurl。

还有其他方法吗? 如果有人实现了此类功能,请告诉我您的方法。

谢谢

【问题讨论】:

    标签: c# asp.net url-rewriting multi-tenant


    【解决方案1】:

    如果您使用的是 ASP.NET 3.5 SP1,那么您应该研究从 MVC 项目引入的新 routing engine。这将是一个干净的解决方案。

    【讨论】:

      【解决方案2】:

      我们正在使用来自http://urlrewriting.net 的 DLL 和类似以下的规则:

      <urlrewritingnet xmlns="http://www.urlrewriting.net/schemas/config/2006/07">
        <rewrites>
          <add name="Customer" virtualUrl="^~/([^/]+)/([^/]+).aspx" destinationUrl="~/$2.aspx?customer=$1"/>
          <add name="CustomerStart" virtualUrl="^~/([^/]+)/$" destinationUrl="~/Default.aspx?customer=$1"/>
          <add name="CustomerStartAddSlash" virtualUrl="^http\://([^/]+)/([a-zA-Z0-9_-]+)$"
                                            destinationUrl="http://www.example.com/$2/"
                                            redirect="Domain" redirectMode="Permanent" />
        </rewrites>
      </urlrewritingnet>
      

      这些规则执行以下映射。这些是重写,所以用户总是在他的浏览器中看到左边的 URL:

      Rule 1: http://www.example.com/customerA/something.aspx => http://www.example.com/something.aspx?customer=customerA
      Rule 2: http://www.example.com/customerA/ => http://www.example.com/Default.aspx?customer=customerA
      

      第三条规则是重定向而不是重写,即它确保在用户的浏览器中添加尾部斜杠(确保相对路径正常工作):

      Rule 3: http://www.example.com/customerA => http://www.example.com/customerA/
      

      【讨论】:

      • 我尝试使用您提供的方法,但我更愿意为此编写自己的代码,
      【解决方案3】:

      看看thesequestions
      你的方法有一个名字。它叫做Multitenancy

      【讨论】:

      • 我没有找到上面给出的任何解决方案完全符合我的要求,我为此编写了自己的逻辑,它使用 Global.ascx 的 BeginRequest、登录页面、基本页面和为 Response.Redirect 创建的公共类.我不再直接使用 Asp.Net 的 Response.Redirect、Paths 和 Session 变量,而是在它们之上创建了包装器以将 companyName 从 url 添加到 Paths。
      【解决方案4】:

      我没有找到任何完全符合我要求的解决方案,我为此编写了自己的逻辑,它使用 Global.ascx 的 BeginRequest、登录页面、基本页面和为Response.Redirect 创建的通用类。我不再直接使用 Asp.Net 的 Response.Redirect、Paths 和 Session 变量,而是在它们之上创建了包装器,以将 companyName 从 url 添加到 Paths。

      如果您需要有关我的代码的更多信息,请告诉我

      欢迎其他答案。

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-26
        • 2019-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多