关于MVC路由的困惑

时间:2010-04-18 11:16:00

标签: asp.net-mvc routes

下面的问题是什么?

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

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

当我导航到/ home / index时,“id”参数采用默认值“test”,但是当我导航到home / report时,name参数为null。

简而言之,如果路由定义是路由表中的第一个,则参数将采用其默认值。但下面的其他人没有。

2 个答案:

答案 0 :(得分:0)

这两条路线{controller}/{action}/{id}{controller}/{action}/{name}含糊不清。它无法区分/home/index/id/home/report/abc,它始终是路径定义中的第一条路线,因为在第二种情况下它会认为id = "abc"

答案 1 :(得分:0)

使用Phil Haack路由调试器..以更清楚地了解您的路由如何在不同的路径上做出反应。

download

相关问题