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

依然范特西╮ 提交于 2019-12-01 04:53:39

问题


At the moment I have just this route defined

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

I want to map the /Home/Action1/id to just /Action1/id anybody knows how ?


回答1:


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.




回答2:


look at this question How to Route /About to /Home/About

It's not (exactly) the answer but I think it gives you the answer in reverse.




回答3:


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

try that, as long as you default the controller and action it should work. Don't forget when generatig links with Html.Action to use the new route name.

Apologies for formatting written on my iPhone



来源:https://stackoverflow.com/questions/2832202/how-to-map-home-action-id-to-just-action-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!