ASP.NET MVC路由如何工作? [英] How do ASP.NET MVC Routes work?

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

问题描述

我定义了以下路由:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = "" }
    );

    // Added custom route here!
    routes.MapRoute(
        "CatchAll", 
        "{*catchall}," 
        new { controller = "Error", action = "NotFound" }
    );
}

没有新内容-这是默认的ASP.NET MVC1 RegisterRoutes 方法,并添加一个自定义路由。

nothing new - that's the default ASP.NET MVC1 RegisterRoutes method, with one custom route added.

现在,如果我转到以下网址,则会得到404 ...

Now, if I goto the following url, i get a 404...

http://whatever/Home/MissingActionMethod

因此在 HomeController中没有名为 MissingActionMethod ActionMethod 。所以,这是否意味着,如果我转到上面定义的第一条路线,而无法找到一个动作..然后我又回来尝试第二条路线吗?冲洗重复吗?

So there's no ActionMethod called MissingActionMethod in the HomeController. So, does this mean, if i goto the 1st route defined, above .. and fail to find an action .. do I then come back and try the second route? rinse-repeat?

或者一旦我匹配一条路线,我便尝试并执行该路线..如果我失败了(即,找到该动作不见了..然后..运气不好?

Or once i match a route, i then try and execute that route .. and if i fail (ie, find the action is missing) .. then .. bad luck? boomski?

欢呼!

感谢回复,但是他们没有正确阅读我的问题:(我知道
1)路由顺序很重要
b)哈克的路由调试器

Thanks heaps for the replies, but they are not reading my question properly :( I know 1) order of routes are important b) haack's route debugger

但是我的问题不是这个。我问..如果第一条路线是已处理的..但失败了..然后它会从列表中移至下一条吗?

but my question is not about that. I'm asking that .. if the first route is 'handled' .. but fails .. does it then go down the list to the next one?

所以,在上面的例子中。第一个称为默认的路由与请求的网址/资源相匹配...但是,当框架尝试查找某个动作时,该路径缺少404。

So, in my example above. The first route called 'Default' is matched against the url/resource requested ... but when the framework tries to find an action, which is missing .. it 404's.

..这是否意味着框架首先匹配默认路由..尝试它..失败..回到路由列表..试图找到匹配..的 next 路由最终失败,因此它放弃了?

So .. does that mean the framework first matches the "default" route .. tries it .. fails .. goes BACK to the route list .. tries to find the next route that matches .. and finally fails so it then gives up?

或者它只找到第一个且只有第一个路由匹配..如果找不到控制器和/或动作..那就放弃了,然后呢? (这是我怀疑)。如果是的话..那么如何找到404?

Or it only finds the first and only the first route it matches .. and if it fails to find the controller and/or action .. then it just gives up there and then? (This is what i suspect). And if so .. how does it then figure out how to 404?

< a href = http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx rel = noreferrer>菲尔·哈克(Phil Haack)实际上在谈论我的问题,有点...但是没有回答我好奇的部分->如何以及在何处确定找不到404资源。

Phil Haack actually talks about my question, a bit ... but doesn't answer the part I was curious about -> how and where it determines a 404 resource not found.

推荐答案

路线!=动作。

是这样的-在传入请求时,路由模块在匹配的路由表中搜索第一个路由,然后尝试

It goes like this - on incoming request, routing module searches for first route in route table that matches and then tries to call appropriate action.

如果未找到操作,请求将失败并返回404(它不会尝试寻找下一条路线)。

If action is not found, request fails and returns 404 (it does NOT try to look for next route).

但是应该可以扩展框架来实现这一目标。我的第一个猜测-您可以编写自己的 RouteHandler

But it should be possible to extend framework in order to achieve this. My first guess - You could write your own RouteHandler.



  1. RouteHandler

    RouteHandler并不是特定于ASP.NET MVC的组件,它决定了选择路由后要做什么。显然,如果更改RouteHandler,最终将在没有ASP.NET MVC的情况下处理请求,但是如果您想直接使用某些特定的HttpHanlders甚至使用经典的WebForm处理路由,这将很有用。


无论如何-我不建议这样做。最好保持路由哑巴。

Anyway - I wouldn't recommend it though. It's better to keep routing dumb.

经过一番快速搜索之后,我对此并不感到乐观。 :)

After some quick googling - I'm not so optimistic about this anymore. :)

这篇关于ASP.NET MVC路由如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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