Asp.net WebAPI Attribute Rout

方式一: AttributeRouting(第三方)

主要是實現Web Api Attribute路由的功能 對應的還有 Mvc 版。 AttributeRouting for GitHubhtml

參考(不是很好): http://www.cnblogs.com/n-pei/archive/2012/07/17/2595352.htmlgit

安裝github

Install-Package AttributeRouting.WebApi

[Get("Link")]web

http://localhost:0001/link getapi

[Get("Link/Oschina")]asp.net

http://localhost:0001/Link/Oschina getpost

[Post("LinkPost")].net

http://localhost:0001/link postcode

[Put("LinkPost")]htm

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

謝謝

相關文章
相關標籤/搜索