且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态加载的模块特定租户的路线

更新时间:2023-12-05 08:47:46

如何将每个网站在您的应用程序区分开来?如果我们假设每个用户将一个独特的域名或子域名标识,那么你可以用一条路线,有些 RouteConstraints 完成你的路由。创建两个限制,一个是控制器,其它为行动。假设你会在你的数据库中的表其中列出特定租户可用控制器/动作,你的约束将是如下:

How will each website be distinguished in your app? If we assume each tenant will be identified by a unique domain name or subdomain name, then you can accomplish your routing with one route and some RouteConstraints. Create two constraints, one for controllers, the other for actions. Assuming that you will have tables in your database which list the available controllers/actions for a specific tenant, your constraints would be as follows:

using System; 
using System.Web; 
using System.Web.Routing;  

namespace ExampleApp.Extensions 
{ 
    public class IsControllerValidForTenant : IRouteConstraint
    {
        public IsControllerValidForTenant() { }

        private DbEntities _db = new DbEntities();

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            // determine domain
            var domainName = httpContext.Request.Url.DnsSafeHost;
            var siteId = _db.Sites.FirstorDefault(s => s.DomainName == domainName).SiteId;
            // passed constraint if this controller is valid for this tenant
            return (_db.SiteControllers.Where(sc => sc.Controller == values[parameterName].ToString() && sc.SiteId == siteId).Count() > 0);
        }
    }

    public class IsActionValidForTenant : IRouteConstraint
    {
        public IsActionValidForTenant() { }

        private DbEntities _db = new DbEntities();

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            // determine domain
            var domainName = httpContext.Request.Url.DnsSafeHost;
            var siteId = _db.Sites.FirstorDefault(s => s.DomainName == domainName).SiteId;
            // passed constraint if this action is valid for this tenant
            return (_db.SiteActions.Where(sa => sa.Action == values[parameterName].ToString() && sa.SiteId == siteId).Count() > 0);
        }
    }
}

然后,在Global.asax.cs中,定义您的路线如下:

Then, in Global.asax.cs, define your route as follows:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new { controller = new IsControllerValidForTenant(), action = new IsActionValidForTenant(),}
    );
}

当一个请求时,该限制将检查控制器和动作是否有效的领域,因此,只有为租户有效的控制器和动作将通过RouteConstraints。

When a request comes in, the constraints will examine whether the controller and action are valid for the domain, so that only valid controllers and actions for that tenant will pass the RouteConstraints.