简体   繁体   English

属性路由未触及控制器内部的动作方法

[英]Attribute routing not hitting the action methods inside controllers

I'm using attribute routing in my MVC5 application and it is working fine. 我在MVC5应用程序中使用属性路由,并且工作正常。 When I tried to create an area and placed attribute routing on the controller inside it, it returns 404. 当我尝试创建区域并将属性路由放置在其中的控制器上时,它返回404。

I know, to enable attribute routing inside Area, I have to use [RouteArea("Area Name Here")] and also have to add routes.MapMvcAttributeRoutes(); 我知道,要在Area内启用属性路由,我必须使用[RouteArea("Area Name Here")] ,还必须添加routes.MapMvcAttributeRoutes(); [RouteArea("Area Name Here")] inside my RouteConfig class. 在我的RouteConfig类中。 I did all this and designed my controller like this: 我做了所有这些,并像这样设计了控制器:

[RouteArea("Client")]
[RoutePrefix("Client")]
public class ClientController : Controller
{
    #region Properties
    private readonly string apiUrl = ConfigurationManager.AppSettings["apiUrl"];
    #endregion

    #region Constructor
    public ClientController()
    {

    }
    #endregion

    #region Action Methods
    [HttpGet, Route("create")]
    public ActionResult Index()
    {
       --logic here
    }
    #endregion
 }

When i run the application using the route: http://localhost:26189/Client/create , I'm able to hit the constructor but not the Index method. 当我使用以下路径运行应用程序时: http://localhost:26189/Client/create ,我能够访问构造函数,但不能访问Index方法。 Interestingly, if i remove the attribute [HttpGet, Route("create")] and try this route http://localhost:26189/Client/Index it will hit the Index method. 有趣的是,如果我删除属性[HttpGet, Route("create")]并尝试使用此路由http://localhost:26189/Client/Index ,它将命中Index方法。

I'm going through these links but not found the exact fix: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/#route-areas 我正在浏览这些链接,但未找到确切的解决方法: https : //blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/#route -areas

You likely have ordering problems in your startup path. 您的启动路径中可能有订购问题。 Make sure things are called in this order: 确保按以下顺序调用:

  1. AreaRegistration.RegisterAllAreas();
  2. routes.MapMvcAttributeRoutes();
  3. Any convention-based routes 任何基于约定的路由

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM