0
votes

In CardsController, I have two post method written as below. I tried with /api/cards/card or /api/cards/cardlist and get exception message such as

"Multiple actions were found that match the request: \r\nSystem.Net.Http.HttpResponseMessage PostValue(Card) on type WebApiService.Controllers.CardsController\r\nSystem.Net.Http.HttpResponseMessage PostValue(CardList) on type WebApiService.Controllers.CardsController", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at 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 request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)"

<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

In the Web.Config is configured as below.

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
did you try changing routes order? First "api/{controller}/{action}/{id}" and then "api/{controller}/{id}" ? Yes, change Web.Config and put first the route with more params. - Mate
@Mate Thanks alot. Can you post your comment as answer. Need to mark your answer. Erm, why is the routes order is causing the error message? Is the service still RESTFUL when i include the action name in the routes? ( /api/cards/card or /api/cards/cardlist) - belltric
Yes, but why not use /api/cards/list or method GET and invoke /api/cards ? - Mate
Ahhh, was not aware of the action name>< Thanks @Mate - belltric
I'm glad to be of help. Check this guide blog.mwaysolutions.com/2014/06/05/… ( cars examples too! ) - Mate

1 Answers

0
votes

Change Web.Config and put first the route with more params

 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} _
    )

first pattern to match against the URL will be action that handles the request. Check Here