現在我正在做WebAPI,因爲我們知道每個控制器都會有自己的網址。可以選擇更改那些我們喜歡的網址。下面的例子是目前Web API的例子。在webapi中自定義數據註解?
Do we really want to use mixed case in urls?
- http://localhost:8282/api/encode/
- http://localhost:8282/api/techdisciplines/
- http://localhost:8282/api/memstatus/
- http://localhost:8282/api/isstaff/
想要將它們更改爲以下url並更改主代碼效果。只需更改數據註釋。
- http://localhost:8282/api/cus/encode/<customer id>
- http://localhost:8282/api/cus/disciplines/<token>
- http://localhost:8282/api/cus/mem_status/<token>
- http://localhost:8282/api/cus/is_staff/<token>
就像Java有自動找到方法。
@Path("customer/{i_Constit}/subscriptions")
public Response getSubscriptions(@PathParam("i_Constit") String customerId)
{
....
...
您可以使用路由屬性中的Web API 2:http://www.asp.net/web-api/overview/web-api -routing-and-actions/attribute-routing-in-web-api-2 – bdoshi