【发布时间】:2016-03-03 19:30:00
【问题描述】:
我在一个区域内有一条自定义路线:
[RouteArea("MyArea")]
public class MyController: Controller
{
[Route("~/my-action")]
public ActionResult MyAction()
{
return View();
}
}
在添加 Route 和 RouteArea 属性之前,我通过以下路由访问了此操作:
~/MyArea/MyController/MyAction
现在我添加了这个属性,我可以通过这个来访问它:
~/my-action
我曾经有一个 ActionLink 指向这个动作,它看起来像这样:
@Html.ActionLink("Link To My Action", "MyAction", "My", new { Area = "MyArea" }, new { })
现在这个 ActionLink 不再起作用了。我该如何解决?
【问题讨论】:
-
你
ActionLink永远不会工作 - 你的控制器被命名为MyController所以它必须是@Html.ActionLink("Link To My Action", "MyAction", "My", new { Area = "MyArea" }, new { }) -
对不起,这只是一个例子,我在更改区域、控制器和动作的名称时犯了一个错误。在我的真实代码中这是正确的,我也在我的问题中修复了它。
-
你正在使用Attribute Routing,你有没有在
RouteConfig.cs文件中这样初始化这个特性:routes.MapMvcAttributeRoutes()在RegisterRoutes方法中? -
是的,我已经初始化了。
-
我之前在同时使用
Traditional Routing和Attribute Routing时遇到了冲突 - 如果您有任何痕迹,可以尝试删除Traditional,然后尝试使用Attributes?
标签: asp.net-mvc routing asp.net-mvc-5