ASP.NET MVC 路由问题?
为什么:
<%= Html.ActionLinkForAreas<UsersController>(c => c.User(), "My Details") %>
生成一个包含以下内容的 URL:
Users/User
但是:
<%= Html.ActionLinkForAreas<BlaController>(c => c.Index(1), "My Bla Di Bla")%>
像这样的 URL:
Bla
而不是这样:
Bla/Index
换句话说,为什么 Index 操作被“吞没”。这是否与如下所示的路由有关:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.RouteExistingFiles = true;
routes.IgnoreRoute("Content/{*wildcard}");
routes.IgnoreRoute("Scripts/{*wildcard}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
或者还有其他原因吗?我怎样才能改变这种行为?谢谢。
最好的祝愿,
克里斯蒂安
Why does:
<%= Html.ActionLinkForAreas<UsersController>(c => c.User(), "My Details") %>
Generate an URL containing this:
Users/User
But:
<%= Html.ActionLinkForAreas<BlaController>(c => c.Index(1), "My Bla Di Bla")%>
An URL like this:
Bla
Rather than this:
Bla/Index
In other words why is the Index action ‘swallowed’. Does this have to do with the routing which looks like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.RouteExistingFiles = true;
routes.IgnoreRoute("Content/{*wildcard}");
routes.IgnoreRoute("Scripts/{*wildcard}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Or is there another reason? How can I change this behaviour? Thanks.
Best wishes,
Christian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您已将
Index
指定为默认操作:action = "Index"
部分。由于它是默认操作,因此每当您创建它的 URL 时,“索引”部分都会被省略。这使您有机会拥有简洁的 URL。顺便说一句,同样的规则也适用于控制器本身。如果您路由到“Home”控制器,则指向它的 URL 将省略“Home”部分,从而允许您拥有原始基本 URL,例如“/”。Becaues you've specified
Index
as your default action:The
action = "Index"
part. Since it's the default action, whenever your create URLs to it, the "Index" part will be omitted. This affords you the opportunity to have nice concise URLs. Incidentally, the same rule applies to the controller itself. If you route to the "Home" controller, URLs to it will have the "Home" part elided, thus allowing you to have a raw base URL such as "/".