开发者

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

At the moment I have just this route defined

 routes.MapRoute(
                "Default",                                                
                "{controller}/{action}/{id}",                           
                new { contr开发者_Python百科oller = "Home", action = "Index", id = "" } 
                );

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


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.


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.


 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜