简体   繁体   English

MVC5路由命名空间的优先级

[英]MVC5 routing namespace prioritize

In MVC5 routing I need one situation. 在MVC5路由中,我需要一种情况。 I have 2 namespaces with same controller name. 我有2个具有相同控制器名称的名称空间。 I want to dynamically chose between controllers without conflict 我想在控制器之间动态选择而不发生冲突

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new[] { "Namespace1","Namespace2" });

In here if there are 2 controllers with same name and actions under both namespaces it gives conflict error. 在这里,如果两个名称空间下有两个具有相同名称和动作的控制器,则会产生冲突错误。 What I want to do is prioritize namespaces. 我想做的是优先命名空间。

If both namespaces has Home.Index action I want Namespace1 take the lead instead of error. 如果两个名称空间都具有Home.Index操作,则我希望Namespace1主导而不是出错。 If namespace1 does not have the action I want system check for Namespace2. 如果namespace1没有操作,我要系统检查Namespace2。

I tried few ways but I don't want to use attribute routing better architecture. 我尝试了几种方法,但是我不想使用属性路由更好的体系结构。

We have a BaseECommerce asp.net mvc project and UI projects that are taking inherit of BaseEcommerce project. 我们有一个继承自BaseEcommerce项目的BaseECommerce asp.net mvc项目和UI项目。 Purpose is if there is same Controller and Action in UI project use that. 目的是在UI项目中使用相同的Controller和Action。 Else use base project. 其他使用基础项目。

Thanks for help. 感谢帮助。

You prioritize routes by registering them in the correct order - the first match wins. 您可以按照正确的顺序对路由进行优先级排序-第一场比赛获胜。

You cannot prioritize namespaces. 您不能确定名称空间的优先级。 The namespaces field is for specifying all of the namespaces for a specific route (which is usually a single namespace). 名称空间字段用于指定特定路由的所有名称空间 (通常是单个名称空间)。 These are the set of namespaces where the specific route will scan for controllers. 这些是特定路由将在其中扫描控制器的名称空间集。

So, if you want to avoid a conflict between similar named controllers, you need to have a route for each namespace. 因此,如果要避免相似的命名控制器之间发生冲突,则需要为每个命名空间都有一条路由。 But, since the first matching route always wins, for it to work each controller action will need a unique URL. 但是,由于第一个匹配路由始终会获胜,因此要使其起作用,每个控制器操作都需要一个唯一的URL。

routes.MapRoute(
            "Default", // Route name
            "Namespace2/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] { "Namespace2.Controllers" });

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] { "Namespace1.Controllers" });

There are other ways of making routes match requests than above - for ideas, see this answer . 除了上述之外,还有其他方法可以使路线匹配请求-有关想法,请参见此答案

Note that you could use a custom route constraint to check whether some module is loaded into your application and if so, disable the route. 请注意,您可以使用自定义路由约束来检查是否将某些模块加载到应用程序中,如果已加载,请禁用路由。

public class ModuleNotInstalledConstraint : IRouteConstraint
{
    public bool Match(
        HttpContextBase httpContext, 
        Route route, 
        string parameterName, 
        RouteValueDictionary values, 
        RouteDirection routeDirection)
    {
        if (module is installed)
        {
            return false;
        }

        return true;
    }
}

Usage 用法

    routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new { _ = new ModuleNotInstalledConstraint() },
            new[] { "Namespace1.Controllers" });

This way, the route is ignored if the module is available, so you can use a route with the exact same signature and it will be "overridden" when the module is available. 这样,如果模块可用,则忽略该路由,因此您可以使用具有完全相同签名的路由,并且在模块可用时将被“覆盖”。 But you will still need to specify namespaces to ensure the scan doesn't conflict with controllers that have the same name. 但是,您仍然需要指定名称空间,以确保扫描不会与具有相同名称的控制器冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM