MVC Attribute Routes & Route Inheritance

和自甴很熟 提交于 2019-12-13 00:49:39

问题


I am trying to get attribute routing in MVC5 to let me inherit the routes from a base controller. Example:

public abstract class BaseController: Controller
{
    [Route("")]
    public virtual ActionResult Index()

    [Route("edit/{id}")]
    public virtual ActionResult Edit(TKey id)

    //etc
}

[RoutePrefix("people")]
public class PersonController : BaseController
{
    //etc
}

Then obviously I should have /people for the Index page, /people/edit/1 for the Edit page etc. However, it seems like this is not supported or I just don't know what I'm doing. Is there a way to get this working the way I want?


回答1:


According to this: .NET WebAPI Attribute Routing and inheritance, route attribute cannot be inherited. Apparently, MVC 5.2 will have support for doing this through IDirectRouteProvider.



来源:https://stackoverflow.com/questions/23431043/mvc-attribute-routes-route-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!