首页 > 代码库 > 在webAPI的BaseController上使用RoutePrefix

在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());

  

在webAPI的BaseController上使用RoutePrefix