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

查看:19
本文介绍了.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/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天全站免登陆