Asp.net WebAPI Attribute Rout
方式一: AttributeRouting(第三方)
主要是实现Web Api Attribute路由的功能 对应的还有 Mvc 版。 AttributeRouting for GitHub
参考(不是很好): http://www.cnblogs.com/n-pei/archive/2012/07/17/2595352.html
安装
Install-Package AttributeRouting.WebApi
[Get("Link")]
http://localhost:0001/link get
[Get("Link/Oschina")]
http://localhost:0001/Link/Oschina get
[Post("LinkPost")]
http://localhost:0001/link post
[Put("LinkPost")]
http://localhost:0001/link put
是不是超级方便
方式二(推荐): Attribute Routing in ASP.NET Web API 2 (Microsoft)
安装
Install-Package Microsoft.AspNet.WebApi.WebHost
配置 (MVC 在 RouteConfig.cs | Web Api 在 WebApiConfig.cs)
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
...
);
}
[HttpGet]
[Route("Link/{customerId}/orders")]
http://localhost:0001/link/{customerId}/orders get
[HttpDelete]
[Route("Orders/Delete/{id}")]
http://localhost:0001/Orders/Delete/{id} delete
一定要参考内容
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
谢谢