.Net MVC 路由 Catchall 不起作用 [英] .Net MVC Routing Catchall not working

查看:15
本文介绍了.Net MVC 路由 Catchall 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚这一点.我正在尝试使用 MVC Beta 并尝试实现一个包罗万象的路由,这样如果用户输入 mysite.com/blah 而不是 mysite.com/home/index 它将命中错误"路由.

I can't seem to figure this out. I'm experimenting with MVC Beta and am trying to implement a catchall route such that if the user enters mysite.com/blah instead of mysite.com/home/index it will hit the "Error" route.

不幸的是,默认"路线似乎总是先捕获废话".事实上,我能够到达错误"路线的唯一路线是 blah/blah/blah/blah.

Unfortunately it seems that the "Default" route always catches "blah" first. In fact the only route I've been able to get to the "Error" route with is blah/blah/blah/blah.

这是它应该工作的方式吗,因为我已经看到其他示例具有像这样设置的默认"和错误"路由,并且似乎如果他们要输入一个没有的控制器'不存在它会命中错误"路线.

Is this the way it's supposed to work, because I've seen other examples that have the "Default" and "Error" route set up just like this and it seems that if they were to type in a controller that doesn't exist it would hit the "Error" route.

我是否缺少某些东西(很可能)或者我只需要为每个控制器创建一个特定的路由?

Is there something I'm missing (very possible) or will I just have to create a specific route for each controller?

我正在使用的代码:

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

        routes.MapRoute(
            "Error",
            "{*catchall}",
            new { controller = "Base", action = "Error", id = "404" }
        );

谢谢你,杰夫

推荐答案

MVC 路由按照输入的顺序进行检查.

MVC routes are checked in the order that they are entered.

Mysite/blah 会被默认路由找到.控制器是废话,动作是索引.

Mysite/blah will be found by the default route. The controller will be blah, and the action is index.

当您输入 mysite/blah/blah/blah/blah 路由时,您给了它一个无法将默认路由映射到的路由,然后调用了您的包罗万象的路由.

When you entered the mysite/blah/blah/blah/blah route you gave it a route it could not map the default route to and then your catchall route was called.

对于其他示例,您是否注意到它们是否设置了一些错误过滤器?我很确定默认的 asp.net mvc 站点已经在页面上有一些错误处理属性.

For those other examples, did you notice if they had some error filters setup? I'm pretty sure the default asp.net mvc site has some error handling attributes on the pages already.

这篇关于.Net MVC 路由 Catchall 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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