如何实现MultiTenant-Asp.net MVC应用程序?

时间:2012-02-08 12:51:59

标签: c# asp.net-mvc-3

我有一个Asp.net MVC3应用程序我希望能够允许多个/不同的客户端访问相同的应用程序,但使用不同的URL。我已经设法配置数据库以允许这个。所以hia是我想在域中托管我的应用程序的主要部分... www.myapplication.com然后允许不同的客户端使用1.www.clientOne.myapplication.com 2.www.clientTwo.myapplication访问同一个应用程序。融为一体

怎么做?请提供代码。感谢。

2 个答案:

答案 0 :(得分:2)

Stack Exchange本身在多租户架构上运行!

http://weblogs.asp.net/zowens/archive/2010/06/16/multi-tenant-asp-net-mvc-views.aspx

这组文章可以帮助您了解设置基本架构所需的内容。它有很多代码,可以很好地覆盖你需要的东西。我不认为你会在这里获得你需要的确切代码来设置你的多天文应用程序的整个核心架构,我建议你使用上面的这篇文章或类似的文章。

答案 1 :(得分:0)

要实现多租户应用程序,您必须在其中做一些技巧

C:\Windows\System32\drivers\etc host file 
通过继承和实现RouteConfig.Cs接口,在IRouteConstraint文件的 App_Start 文件夹中

和一些编码技巧。还需要查看IIS服务器中的绑定设置(为此,您需要查看以下链接中提供的视频教程)

您可以在https://github.com/ashikcse20/ASP-MVC5-MULTI-TENANT-REPOSITORY处获得包含完整书面教程的完整代码。

此处提供了视频教程 ASP .NET MVC 5 Multi Tenant Example With Basic Code (Single Database Per Tenant)

RegisterRoutes功能的RouteConfig.Cs中的代码

routes.MapRoute(
            name: "Default", url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            constraints: new { TenantRouting = new RoutingConstraint() }
                 );

继承并实现IRouteConstraint接口

 public class RoutingConstraint : IRouteConstraint // It is main Class for Multi teanant
 { 

      public bool Match(HttpContextBase httpContext, Route route, string getParameter, RouteValueDictionary values, RouteDirection routeDirection)
      {
            // Got htis code from  http://blog.gaxion.com/2017/05/how-to-implement-multi-tenancy-with.html
            var GetAddress = httpContext.Request.Headers["Host"].Split('.'); 
            var tenant = GetAddress[0];
            //Here you can apply your tricks and logic. Note for when you put it in public server then www.hamdunsoft.com , www.tenant1.hamdunsoft.com then you need to change a little bit in the conditions . Because a www. was added.
            if (GetAddress.Length < 2) // See here for localhost:80 or localhost:9780 ohh also for hamdun soft  execution will enter here . But for less than 2? will hamdunsoft.com enter here?
            {
                 tenant = "This is the main domain";

                 Constant.DatabaseName = "TEST";
                 if (!values.ContainsKey("tenant"))
                      values.Add("tenant", tenant);
                 //return false;
                 // return true;
            }
             else if (GetAddress.Length == 2) //   execution will enter here  for  hamdunsoft.com enter here but not for www.hamdunsoft.com
            {
                 tenant = "This is the main domain";

                 Constant.DatabaseName = GetAddress[0];  
                 if (!values.ContainsKey("tenant"))
                      values.Add("tenant", tenant);
                 //return false;
                 // return true;
            }
            else if (!values.ContainsKey("tenant")) // for tenant1.hamdunsoft.com execution will enter here
            {
                 values.Add("tenant", tenant);
                 Constant.DatabaseName = GetAddress[1]+"."+ tenant;
            }

            return true;
      }
 }
相关问题