净MVC路由无限别名不工作 [英] .Net MVC Routing Catchall not working

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

问题描述

我似乎无法想出解决办法。我与MVC的测试实验和想实现一个包罗万象的路线,这样如果用户输入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.

不幸的是它似乎是默认的路线始终抓住嗒嗒之首。事实上的唯一途径,我已经能够获得与错误的路线是胡说/等等/等等/等等。

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.

这是它应该工作的方式,因为我已经看到了有Default和错误的路线设置就这样,似乎如果他们要在键入一个控制器,没有按其他的例子ŧ存在,会打出了错误的路线。

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?

code我使用的是:

Code I'm using:

        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" }
        );

感谢您,
杰夫

Thank you, Jeff

推荐答案

MVC路线的,因为它们的输入顺序检查。

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

MYSITE /嗒嗒将被默认路由被发现。控制器将嗒嗒,动作是索引。

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

当您输入的mysite /等等/等等/等等/等等路线,你给它一个路径它可能不是缺省路由映射到,然后你的包罗万象的路线被称为

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.

对于其他的例子,你有没有注意到,如果他们有一些错误过滤器的设置?我是pretty确保默认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.

这篇关于净MVC路由无限别名不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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