MVC 自定义路由子域

MVC Custom Routing Subdomain

我正在尝试构建附加到 MVC 区域的“租户”子域路由。在这种情况下,我有一个名为“租户”的区域,它有两个控制器; Public 和管理员。我的自定义路由用于抓取匹配的子域,然后将它们路由到正确的控制器操作区域。

本项目的基础来自以下 http://www.matrichard.com/post/asp.net-mvc-5-routing-with-subdomain

我遇到的问题是在自定义子域路由中。当我点击 Public/Index 路线时,routeData 返回 null,我看到以下错误。虽然如果路线是 /admin 它 returns 正确的 routeData.

Server Error in '/' Application.

The matched route does not include a 'controller' route value, which is required.

它似乎也总是使用 RouteDebugger 工具进行匹配,这是我问题的线索吗?

示例路线:

controller=Public action=Index, area=Tenant

http://tenant1.mydomain.com:8080/

http://tenant1.mydomain.com:8080/logon

controller=Admin action=Index, area=Tenant

http://tenant1.mydomain.com:8080/admin

http://tenant1.mydomain.com:8080/admin/edit

--

SubdomainRouteP.cs

public class SubdomainRouteP : Route
{
    public string Domain { get; set; }

    public SubdomainRouteP(string domain, string url, RouteValueDictionary defaults): this(domain, url, defaults, new MvcRouteHandler())
    {
    }

    public SubdomainRouteP(string domain, string url, object defaults): this(domain, url, new RouteValueDictionary(defaults), new MvcRouteHandler())
    {
    }

    public SubdomainRouteP(string domain, string url, object defaults, IRouteHandler routeHandler): this(domain, url, new RouteValueDictionary(defaults), routeHandler)
    {
    }

    public SubdomainRouteP(string domain, string url, RouteValueDictionary defaults, IRouteHandler routeHandler): base(url, defaults, routeHandler)
    {
        this.Domain = domain;
    }

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        // 
        // routeData object returns null in some cases 
        // 
        var routeData = base.GetRouteData(httpContext);

        var subdomain = httpContext.Request.Url.Host.Split('.').First();

        string[] blacklist = { "www", "mydomain", "localhost" };

        // This will ignore anything that is not a client tenant prefix
        if (blacklist.Contains(subdomain))
        {
            return null; // Continue to the next route
        }

        // Why is this NULL?
        if (routeData == null)
        {
          
            routeData = new RouteData(this, new MvcRouteHandler());
           
        }

        routeData.DataTokens["Area"] = "Tenant";
        routeData.DataTokens["UseNamespaceFallback"] = bool.FalseString;
        routeData.Values.Add("subdomain", subdomain);

        // IMPORTANT: Always return null if there is no match.
        // This tells .NET routing to check the next route that is registered.
        return routeData;
    }

}

RouteConfig.cs

        routes.Add("Admin_Subdomain", new SubdomainRouteP(
            "{client}.mydomain.com", //of course this should represent the real intent…like I said throwaway demo project in local IIS
            "admin/{action}/{id}",
            new { controller = "Admin", action = "Index", id = UrlParameter.Optional }));

        routes.Add("Public_Subdomain", new SubdomainRouteP(
            "{client}.mydomain.com", //of course this should represent the real intent…like I said throwaway demo project in local IIS
            "{controller}/{action}/{id}",
            new { controller = "Public", action = "Index", id = UrlParameter.Optional }));

        // This is the MVC default Route
        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional });

下面的 Url 给出了 RouteDebugger 的以下结果。在测试 1 和 2 期间,路由仍然匹配 /admin.

测试 1 失败:http://tenant.mydomain.com/

测试 2 失败:http://tenant.mydomain.com/logon

成功3次:http://tenant.mydomain.com/admin

Matches Url Defaults

True admin/{action}/{id} controller = Admin, action = Index

True {controller}/{action}/{id} controller = Public, action = Index

您链接到的post有一个错误:当约束或URL不匹配时,base.GetRouteData方法将returnnull.在这种情况下,将子域名添加到路由字典中显然会抛出异常。该行之前应该有一个空保护子句。

public override RouteData GetRouteData(HttpContextBase httpContext)
{
    var routeData = base.GetRouteData(httpContext);
    if (routeData != null)
    {
        routeData.Values.Add("client", httpContext.Request.Url.Host.Split('.').First());
    }
    return routeData;
}

您的路线也应如此。在基础 class return 为 null 的情况下,您需要确保 return null(这表明 URL 或约束不匹配,我们需要跳过处理这条路线)。

此外,我不确定它与直接将数据添加到 DataTokens 是否有任何区别,但是 MVC 框架有一个 IRouteWithArea 可以实现来配置 Area 路由适用于。

public class SubdomainRouteP : Route, IRouteWithArea
{
    public string Area { get; private set; }

    public SubdomainRouteP(string area, string url, RouteValueDictionary defaults): this(area, url, defaults, new MvcRouteHandler())
    {
    }

    public SubdomainRouteP(string area, string url, object defaults): this(area, url, new RouteValueDictionary(defaults), new MvcRouteHandler())
    {
    }

    public SubdomainRouteP(string area, string url, object defaults, IRouteHandler routeHandler): this(area, url, new RouteValueDictionary(defaults), routeHandler)
    {
    }

    public SubdomainRouteP(string area, string url, RouteValueDictionary defaults, IRouteHandler routeHandler): base(url, defaults, routeHandler)
    {
        this.Area = area;
    }

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var routeData = base.GetRouteData(httpContext);

        // This will ignore anything where the URL or a constraint doesn't match
        // in the call to base.GetRouteData().
        if (routeData != null)
        {
            var subdomain = httpContext.Request.Url.Host.Split('.').First();

            string[] blacklist = { "www", "mydomain", "localhost" };

            // This will ignore anything that is not a client tenant prefix
            if (blacklist.Contains(subdomain))
            {
                return null; // Continue to the next route
            }

            routeData.DataTokens["UseNamespaceFallback"] = bool.FalseString;
            routeData.Values.Add("subdomain", subdomain);
        }

        // IMPORTANT: Always return null if there is no match.
        // This tells .NET routing to check the next route that is registered.
        return routeData;
    }

}

我不知道你想用 domain 参数做什么。 URL 很可能 return something 用于域。因此,您似乎应该在第一个 "{controller}/{action}/{id}" 路由中有一个约束,否则您将永远不会遇到会传递到默认路由的情况。或者,您可以在 URL 中使用一个明确的段,以便您可以区分它(与您对管理路由所做的方式相同)。

routes.Add("Admin_Subdomain", new SubdomainRouteP(
    "Tenant",
    "admin/{action}/{id}",
    new { controller = "Admin", action = "Index", id = UrlParameter.Optional }));

routes.Add("Public_Subdomain", new SubdomainRouteP(
    "Tenant",
    "public/{action}/{id}",
    new { controller = "Public", action = "Index", id = UrlParameter.Optional }));

// This is the MVC default Route
routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional });

另一种选择是添加另一个构造函数参数以传入要检查的有效域的显式列表。