如何实现Multi-Tenan-Asp.net MVC应用程序

本文关键字:Multi-Tenan-Asp net MVC 应用程序 实现 何实现 | 更新日期: 2023-09-27 18:23:40

我有一个Asp.net MVC3应用程序,我希望能够允许多个/不同的客户端访问同一应用程序,但使用不同的url。我已经设法将数据库配置为允许这样做。所以hia是我想在一个领域主持我的应用程序的主要部分,比如。。。www.myapplication.com然后允许不同的客户端使用1.www.clientOne.myapplication.com 2.www.clientWo.myaapplication.com访问同一应用程序。

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

如何实现Multi-Tenan-Asp.net MVC应用程序

Stack Exchange本身运行在多租户体系结构上!

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

这组文章应该会为您提供设置基本架构所需的内容。它有很多代码,在满足您的需求方面做得很好。我不认为你会在这里得到你需要的确切代码来设置你的multi-tenan应用程序的整个核心架构,我建议使用上面的这篇文章或类似的文章。

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

C:'Windows'System32'drivers'etc host file 

以及通过继承和实现CCD_ 2接口在CCD_ 1文件的App_Start文件夹中的一些编码技巧。还需要查看IIS服务器中的绑定设置(为此,您需要查看下面链接中提供的视频教程)

你可以在这里获得完整的代码https://github.com/ashikcse20/ASP-MVC5-MULTI-TENANT-REPOSITORY有完整的书面教程。

视频教程在这里给出ASP.NETMVC 5多租户示例与基本代码(每个租户单个数据库)

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;
      }
 }