带有两个POST方法的Web Api控制器

时间:2017-01-09 05:09:58

标签: vb.net asp.net-web-api

在CardsController中,我有两个post方法,如下所示。我尝试使用/ api / cards / card或/ api / cards / cardlist并获取异常消息,例如

  

"找到了与请求匹配的多个操作:   \ r \ nSystem.Net.Http.HttpResponseMessage类型的PostValue(卡)   WebApiService.Controllers.CardsController \ r \ nSystem.Net.Http.HttpResponseMessage   类型上的PostValue(CardList)   WebApiService.Controllers.CardsController"," ExceptionType":   " System.InvalidOperationException"," StackTrace":"在   System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext   controllerContext)\ r \ n at   System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext   controllerContext)\ r \ n at   System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext   controllerContext,CancellationToken cancellationToken)\ r \ n at   System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage   请求,取消语音取消语言)\ r \ n at   System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage   请求,取消语音取消语言)"

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

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

在Web.Config配置如下。

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

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

1 个答案:

答案 0 :(得分:0)

更改Web.Config并首先放置具有更多参数的路径

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

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

与URL匹配的第一个模式将是处理请求的操作。 Check Here