【问题标题】:Attribute routing not hitting the action methods inside controllers属性路由未命中控制器内的操作方法
【发布时间】:2017-07-03 17:24:48
【问题描述】:

我在我的 MVC5 应用程序中使用属性路由,它工作正常。当我尝试创建一个区域并在其中的控制器上放置属性路由时,它返回 404。

我知道,要在 Area 内启用属性路由,我必须使用 [RouteArea("Area Name Here")] 并且还必须在我的 RouteConfig 类中添加 routes.MapMvcAttributeRoutes();。我做了所有这些,并像这样设计了我的控制器:

[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
 }

当我使用路由:@987654321@ 运行应用程序时,我可以点击构造函数,但不能点击 Index 方法。有趣的是,如果我删除属性 [HttpGet, Route("create")] 并尝试这条路线 @987654322@ 它将命中 Index 方法。

我正在浏览这些链接,但没有找到确切的解决方法: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/#route-areas

【问题讨论】:

  • 我猜你还需要像 [ActionName("create")] 这样的 ActionName 属性来进行索引操作。
  • @Amit - 我尝试添加 thi 属性但没有运气:(
  • 其他地方是否有覆盖您的路由定义?
  • routearea 是做什么的?,看起来调用会变成“/Client/Client/create”?

标签: c# asp.net-mvc asp.net-mvc-routing url-routing attributerouting


【解决方案1】:

您的启动路径中可能存在排序问题。确保按以下顺序调用事物:

  1. AreaRegistration.RegisterAllAreas();
  2. routes.MapMvcAttributeRoutes();
  3. 任何基于约定的路线

【讨论】:

  • 试过了还是没能解决问题:(
猜你喜欢
  • 1970-01-01
  • 2017-06-09
  • 2019-10-03
  • 2019-06-26
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
相关资源
最近更新 更多