【发布时间】: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