【问题标题】:Custom route inside Area's controller action is not working in ASP.NET MVC区域控制器操作中的自定义路由在 ASP.NET MVC 中不起作用
【发布时间】:2015-11-27 10:10:54
【问题描述】:

我正在使用 ASP.NET MVC。我在一些操作上使用了自定义路由。

示例:

/Home/Contact

这个默认路由有一个自定义路由,定义如下:

[Route("~/contact-us")]
public ActionResult Contact()
{
    // Some code.. :)    
}

现在我可以通过 /contact-us 路由访问 Contact Action。

不幸的是,当我尝试在区域内执行相同操作时,这不起作用。当我在区域中的控制器内的操作中添加自定义路由并尝试访问它时,该操作未执行并且我被重定向到我的全局 Home Controller 的 Index 操作。为什么会发生这种情况,我该如何解决?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5 url-routing area


    【解决方案1】:

    您可能应该在Area 中为您的控制器使用RouteArea 属性。以下代码 sn -p 可能会对您有所帮助。

     [RouteArea("YourArea")]  //use your Area name 
     public class ProfileController : Controller
     {
        [Route("~/contact-us")]
        public ActionResult Contact()
        {
            return View();
        }
     }
    

    【讨论】:

      猜你喜欢
      • 2021-03-24
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 2012-08-14
      • 2017-02-21
      相关资源
      最近更新 更多