How to map Home/Action/id to just action/id?

后端 未结 3 1443
谎友^
谎友^ 2021-01-13 13:26

At the moment I have just this route defined

 routes.MapRoute(
                \"Default\",                                                
                \         


        
3条回答
  •  渐次进展
    2021-01-13 14:00

    You may have to be careful with a route like you're asking for because it may catch more than is intended.

    One workaround is to restrict that route by using constraints if possible.

    E.g. Check that id is numeric...

    routes.MapRoute("ActionRoute", 
             "{action}/{id}",
             new { controller = "Home", action = "Index", id="1" },
             new { id = @"\d{1,6}" }
    );
    

    That way you can still specify your generic default route at the end to catch the rest of the routes on the site if needed.

提交回复
热议问题