Mvc-WebAPI特性路由(自定義路由)Demo
Demo由VS2017編寫。
1.先建一個WebApi項目
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
2.WebApiConfig.cs須要註冊特性路由,config.MapHttpAttributeRoutes();
![](http://static.javashuo.com/static/loading.gif)
3.項目默認有2個Controller,我暫時不動默認的了,新建一個WebApiController:TestController
![](http://static.javashuo.com/static/loading.gif)
4.在TestController 類名上加上RoutePrefix特性,api前綴;在下面寫一個測試的User類,後面會用到;
在TestController 寫兩個方法GetUser 、AddUser,分別在方法上面加上Route特性,傳入自定義路由路徑。
![](http://static.javashuo.com/static/loading.gif)
6.完成,F5運行,調用API接口,測試是否能用。
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
7.OK,調用自定義路徑API正常。
歡迎關注本站公眾號,獲取更多信息