在webAPI的BaseController上使用RoutePrefix

  在webAPI2.2中有支持,参考地址:https://docs.microsoft.com/en-us/aspnet/web-api/overview/releases/whats-new-in-aspnet-web-api-22#ARI,具体做法:

public class BaseController : ApiController
{
[Route("{id:int}")]
public string Get(int id)
{
return "Success:" + id;
}
}
[RoutePrefix("api/values")]
public class ValuesController : BaseController
{
}
public class CustomDirectRouteProvider : DefaultDirectRouteProvider
{
protected override IReadOnlyList<IDirectRouteFactory>
GetActionRouteFactories(HttpActionDescriptor actionDescriptor)
{
return actionDescriptor.GetCustomAttributes<IDirectRouteFactory>
(inherit: true);
}
} ///WebApiConfig.cs中加上这个配置
config.MapHttpAttributeRoutes(new CustomDirectRouteProvider());

  

上一篇:Linux常用命令大全(非常全!!!)


下一篇:修改ScriptBundle 生成顺序