【问题标题】:MVC 4: add "Index" to url pathMVC 4:将“索引”添加到 url 路径
【发布时间】:2013-04-02 09:28:53
【问题描述】:
出于某些特定原因,我需要 .NET MVC 4 不要自动从 URL 中删除“索引”。基本上我需要转换
http://example.com/ 到 http://example.com/Index
或
http://example.com/foo 到 http://example.com/foo/Index
问题是@URL.Action("Index", "Foo") 只是输出/Foo,我需要它来输出Foo/Index。
任何帮助将不胜感激!
【问题讨论】:
标签:
asp.net-mvc
razor
asp.net-mvc-4
asp.net-mvc-routing
【解决方案1】:
您应该能够从路由映射中删除默认操作。
所以不要这样:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
只要拿出action = "Index":
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);