开发者

Can i have the following route in ASP.NET MVC :: http://whatever/controller/action.format

开发者 https://www.devze.com 2022-12-23 13:28 出处:网络
is it possi开发者_运维百科ble to add a dot format / .format to the end of any route in ASP.NET MVC .. with a default being html or something?

is it possi开发者_运维百科ble to add a dot format / .format to the end of any route in ASP.NET MVC .. with a default being html or something?

and of course the .format is an optional value.

sample routes:-

http://whatever/controller/action.format
http://whatever/controller/action

those goto the same action method.

cheers!


I'm pretty sure the only clean way to do this would be with two route definitions:

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

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

Make sure you maintain the same order in your route definition list as the "Default2" route will override the "Default" route in all cases if placed before it.

0

精彩评论

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