2017-01-10 83 views
0

基於下面的代碼,我能夠調用/ API /卡/ API /卡/清單,但現在/ API /卡/ 1234567890網頁API控制器與多個POST和單put方法

WebApiConfig

config.Routes.MapHttpRoute(_ 
     name:="WithActionAndIdApi", _ 
     routeTemplate:="api/{controller}/{action}/{id}", _ 
     defaults:=New With {.id = RouteParameter.Optional} _ 
    ) 

    config.Routes.MapHttpRoute(_ 
     name:="WithIdApi", _ 
     routeTemplate:="api/{controller}/{id}" _ 
    ) 

    config.Routes.MapHttpRoute(_ 
     name:="DefaultApi", _ 
     routeTemplate:="api/{controller}", _ 
     defaults:=New With {.action = "DefaultAction"} _ 
    ) 

CardsController

<HttpPost()> 
<ActionName("DefaultAction")> 
Public Function PostValue(<FromBody()> ByVal value As Card_POST.Card) As HttpResponseMessage 

<HttpPost()> 
<ActionName("list")> 
Public Function PostValue(<FromBody()> ByVal value As Cards_POST.CardList) As HttpResponseMessage 

<HttpPut()> 
Public Function PutValue(ByVal Id As String, <FromBody()> ByVal value As Card_PUT) As HttpResponseMessage 
+0

請確保您提出問題。目前還不清楚你有什麼問題或你正試圖解決什麼問題。 – chambo

+0

你如何做PUT請求? – Mate

+0

@Mate,我通過更改我的config.Routes – belltric

回答

1

我已經改變了我config.Routes到下面的代碼,它是能夠支持上面我的情況。

config.Routes.MapHttpRoute(_ 
     name:="DefaultApiWithId", _ 
     routeTemplate:="api/{controller}/{id}", _ 
     defaults:=New With {.id = RouteParameter.Optional}, _ 
     constraints:=New With {.id = "^\d+$"} _ 
    ) 

    config.Routes.MapHttpRoute(_ 
     name:="DefaultApiWithAction", _ 
     routeTemplate:="api/{controller}/{action}" _ 
    ) 

    config.Routes.MapHttpRoute(_ 
     name:="DefaultApiGet", _ 
     routeTemplate:="api/{controller}/{id}", _ 
     defaults:=New With {.id = RouteParameter.Optional}, _ 
     constraints:=New With {.httpMethod = New HttpMethodConstraint({"GET"})} _ 
    ) 

    config.Routes.MapHttpRoute(_ 
     name:="DefaultApiPost", _ 
     routeTemplate:="api/{controller}", _ 
     defaults:=New With {.action = "DefaultPostAction"}, _ 
     constraints:=New With {.httpMethod = New HttpMethodConstraint({"POST"})} _ 
    ) 

    config.Routes.MapHttpRoute(_ 
     name:="DefaultApiPut", _ 
     routeTemplate:="api/{controller}/{id}", _ 
     defaults:=Nothing, _ 
     constraints:=New With {.httpMethod = New HttpMethodConstraint({"PUT"})} _ 
    )