Area 控制器操作中的自定义路由在 ASP.NET MVC 中不起作用
Custom route inside Area's controller action is not working in ASP.NET MVC
我正在使用 ASP.NET MVC。我在我的一些 Actions 上使用了自定义路由。
示例:
/Home/Contact
此默认路由具有如下定义的自定义路由:
[Route("~/contact-us")]
public ActionResult Contact()
{
// Some code.. :)
}
现在我可以通过 /contact-us 路径访问联系操作。
不幸的是,当我尝试在区域内执行相同操作时,这不起作用。当我在某个区域的控制器内的操作中添加自定义路由并尝试访问它时,该操作未执行,我被重定向到全局 Home Controller 的 Index 操作。为什么会发生这种情况,我该如何解决?
可能您应该在 Area
中为您的控制器使用 RouteArea
属性。以下代码片段可能对您有所帮助。
[RouteArea("YourArea")] //use your Area name
public class ProfileController : Controller
{
[Route("~/contact-us")]
public ActionResult Contact()
{
return View();
}
}
我正在使用 ASP.NET MVC。我在我的一些 Actions 上使用了自定义路由。
示例:
/Home/Contact
此默认路由具有如下定义的自定义路由:
[Route("~/contact-us")]
public ActionResult Contact()
{
// Some code.. :)
}
现在我可以通过 /contact-us 路径访问联系操作。
不幸的是,当我尝试在区域内执行相同操作时,这不起作用。当我在某个区域的控制器内的操作中添加自定义路由并尝试访问它时,该操作未执行,我被重定向到全局 Home Controller 的 Index 操作。为什么会发生这种情况,我该如何解决?
可能您应该在 Area
中为您的控制器使用 RouteArea
属性。以下代码片段可能对您有所帮助。
[RouteArea("YourArea")] //use your Area name
public class ProfileController : Controller
{
[Route("~/contact-us")]
public ActionResult Contact()
{
return View();
}
}