当操作等于默认路由值时,MVC ActionLink 会忽略操作
我为我的应用程序定义了以下路由:
routes.MapRoute(
"Referral", // Route name
"{referralCode}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
并且我正在尝试创建一个 ActionLink 来执行 AdminController 上的索引操作:
@Html.ActionLink("admin", "Index", "Admin")
但是,当执行视图时,ActionLink 呈现为(省略索引操作值):
<a href="/Admin">admin</a>
通常是这样没问题,但它会导致与“推荐”路线发生冲突。
注意:如果我使用 ActionLink 来渲染不同的操作,例如“默认”,则 ActionLink 会正确渲染:
<a href="/Admin/Default">admin</a>
“默认”操作正确渲染的事实让我相信问题与为路由指定的默认值。是否有强制 ActionLink 也呈现“索引”操作?
I have the following routes defined for my application:
routes.MapRoute(
"Referral", // Route name
"{referralCode}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
And I'm trying to create an ActionLink to go on the Index action on my AdminController:
@Html.ActionLink("admin", "Index", "Admin")
However, when the view is executed the ActionLink renders as (Index action value is omitted):
<a href="/Admin">admin</a>
Normally this would be ok, but it's causing a collision with the "Referral" route.
NOTE: If I instead use ActionLink to render a different action like "Default," the ActionLink renders correctly:
<a href="/Admin/Default">admin</a>
The fact that the "Default" action renders correctly leads me to believe the problem has to do with the default value specified for the route. Is there anyway to force ActionLink to render the "Index" action as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除默认路由上的默认
action
参数:这将强制始终在 url 中指定操作。
Remove the default
action
parameter on your Default route:That will force the action to always be specified in the url.