MVC路由约束控制器上的名称 [英] MVC Routing Constraint on Controller Names

查看:137
本文介绍了MVC路由约束控制器上的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了类似的路线:

I have routes set up like:

        context.MapRoute(
        name: "Area",
        url: "Area/{controller}/{action}",
        defaults: new
        {
            controller = "Home",
            action = "Dashboard"
        }
        );

        context.MapRoute(
        name: "AccountArea",
        url: "Area/{accountFriendlyId}/{controller}/{action}",
        defaults: new
        {
            controller = "Home",
            action = "Dashboard",
            accountFriendlyId = RouteParameter.Optional
        }
        );

        context.MapRoute(
        name: "AccountCampaignArea",
        url: "Area/{accountFriendlyId}/{campaignFriendlyId}/{controller}/{action}",
        defaults: new
                {
                    controller = "Home", 
                    action = "Dashboard",
                    accountFriendlyId = RouteParameter.Optional,
                    campaignFriendlyId = RouteParameter.Optional
                }
        );

和我有一个强烈的愿望,有区/ friendlyAccountName /首页带我到控制面板(),但这不起作用(404)。我想原因是,我们去寻找一个friendlyAccountName控制器。

And I have a burning desire to have Area/friendlyAccountName/Home take me to the Dashboard() but this doesn't work (404). I think the reason is that we go looking for a friendlyAccountName controller.

舒适的知识,如果我要选择我的名字其中一个控制器后的帐户一切都轰然倒下,是通过有回落至下一路径字符串中没有找到相应的控制器的情况下的一种方式?是否有某种方式使用反射,避免每次都保持约束修改我的控制器的列表?

Comfortable with the knowledge that if I should choose to name an account after one of my controllers everything comes crashing down, is there a way to fall through to the next route in case of a string failing to find a corresponding controller? Is there some way to use reflection and avoid maintaining a constraint every time I modify the list of my controllers?

修改

你知道不使用反射或至少包含派生类型的搜索这方面的方法吗?我不喜欢这招致开销的想法两次当第二路径参数不匹配控制器的名称(通约束,则构建控制器时,再次搜索)。我希望有一种方法来捕获异常,在构建控制器,然后备份,并通过下一个路径落下的地步。

Do you know a way that doesn't use reflection or at least contains the derived type search to this area? I don't like the idea of incurring that overhead twice when the second route parameter does match a controller name (pass constraint then search again when constructing controller). I wish there was a way to catch the exception at the point of constructing a controller and then backing up and falling through to the next route.

推荐答案

为什么需要第一条路呢?如果 {accountFriendlyId} 是可选的,你应该能够忽略它,并得到了相同的路线默认值作为第一个注册的路线。

Why do you need the first route at all? If {accountFriendlyId} is optional, you should be able to omit it and get the same route defaults as your first registered route.

这样的话它会匹配 AccountArea 命名的路由首先,这是你想要的,然后如果一个 {accountFriendlyId} 不指定该区域作为控制器后,会像对待第一个令牌。

That way it would match on the AccountArea named route first, which is what you want, and then if an {accountFriendlyId} isn't specified it would treat the first token after the area as a controller.

其实,我觉得你应该能够完全删除第一个两条路线,只是坚持最后一个,因为前两个路由参数是可选的,默认值是相同的。

In fact, I feel like you should be able remove the first two routes entirely and just stick with the last one, since the first two route parameters are optional and the defaults are identical.

更新

由于 {accountFriendlyId} 可能是一个有效的控制器操作名称,你可以做一对夫妇的其他东西:

Since the {accountFriendlyId} could be a valid controller operation name, you could do a couple of other things:


  1. {accountFriendlyId} 来的路线的结尾,而不是开头。在此之前,最广泛的资源,该资源中的具体细节,更自然的URL风格。

  2. 使用路由约束的。理论上你可以使用反射来生成正则表达式匹配的<控制器名称href=\"http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-custom-route-constraint-cs\">custom约束,或者你也可以手动进行只写出来。事情是这样的:

  1. Move {accountFriendlyId} to the end of the route, instead of at the beginning. This follows a more natural URL style of broadest resource to specific detail within the resource.
  2. Use route constraints. You could theoretically use reflection to generate the regex to match on controller names in a custom constraint, or you could just write them out manually. Something like this:

context.MapRoute(

context.MapRoute(

    name: "Area",
    url: "Area/{controller}/{action}",
    defaults: new
    {
        controller = "Home",
        action = "Dashboard",
        new { controller = @"(Account|Profile|Maintenance)" }
    }

);

这篇关于MVC路由约束控制器上的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆