在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 GetActionRouteFactories(HttpActionDescriptor actionDescriptor) { return actionDescriptor.GetCustomAttributes (inherit: true); } }

///WebApiConfig.cs中加上这个配置
config.MapHttpAttributeRoutes(new CustomDirectRouteProvider());

  

转载于:https://www.cnblogs.com/skybreak/p/6846818.html

你可能感兴趣的:(在webAPI的BaseController上使用RoutePrefix)