我在一个区域内有一条自定义路线:
[RouteArea("MyArea")]
public class MyController: Controller
{
[Route("~/my-action")]
public ActionResult MyAction()
{
return View();
}
}
添加之前路线和 航线区域属性,我通过以下路线访问了这个 Action :
~/MyArea/MyController/MyAction
现在我添加了这个属性,我可以通过这个访问它:
~/my-action
我曾经有一个指向此操作的 ActionLink,它看起来像这样:
@Html.ActionLink("Link To My Action", "MyAction", "My", new { Area = "MyArea" }, new { })
现在此 ActionLink 不再有效。我该如何解决?
最佳答案
我想出了以下解决方案:
我为我创建的路线添加了一个名称。
[Route("~/my-action", Name = "CustomRoute")]
public ActionResult MyAction()
然后我使用了 RouteLink 辅助方法,而不是像这样的 ActionLink 方法:
@Html.RouteLink("Link To My Action", "CustomRoute")
因此,RouteLink 方法的第一个参数是链接的文本,第二个参数是路由的名称 - 与您在 Route 属性中输入的名称相同。
关于asp.net-mvc - 如何在 ASP.NET MVC 中创建到自定义路由的 ActionLink?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33993920/