Asp.net mvc 如何在MVC3中选择路由

Asp.net mvc 如何在MVC3中选择路由,asp.net-mvc,asp.net-mvc-3,razor,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc 3,Razor,Asp.net Mvc Routing,我有三条路线的global.ascx public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "TestRoute", "{id}", new { controller = "Product

我有三条路线的global.ascx

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



        routes.MapRoute(
           "TestRoute",
           "{id}",
           new { controller = "Product", action = "Index3", id = UrlParameter.Optional },
           new { id = @"\d+" } //one or more digits only, no alphabetical characters
       );

        routes.MapRoute(
           "TestCatalogRoute",
           "{id}",
           new { controller = "Home", action = "Index", id = UrlParameter.Optional }
       );

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


    }
当我输入url时:

http://mydomain.com/
它使用TestCatalogRoute路由,但我想要默认路由T.T

如何:

使用url:http://mydomain.com 它使用默认路由 使用url:http://mydomain.com/1 它使用TestRoute它已经完成了! 使用url:http://mydomain.com/abc 它使用TestCatalogRoute路由
删除id=urlparmeter。对于TestCatalogRoute是可选的,然后更改路由的顺序。routehandler将验证每个路由,首先选择匹配的路由。所以,如果你把第二个放在最后,你应该会没事吧?

我建议使用路由调试器来调试你的路由,这很容易使用


这很好,但我认为默认路线终于找到了:你是对的,虫族是最好的答案。如果没有id参数,那么第二条路线是无用的,因此它不应该是可选的。