C Sharper C Sharper - 8 months ago 49
C# Question

web api multiple post methods with different parameter type

Can I have 2 post methods with different datatypes as below :-

[HttpPost]
public HttpResponseMessage Post([StringBody]string data)
{
// Logic
}

[HttpPost]
public HttpResponseMessage Post(Requirements objRequirement)
{
//Logic
}


I am getting below error in postman :-

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Multiple actions were found that match the request:
System.Net.Http.HttpResponseMessage Post(Newtonsoft.Json.Linq.JObject) on type ATL.Trebuchet.RestApi.Controllers.ValuesController
System.Net.Http.HttpResponseMessage Post(ATL.Trebuchet.RestApi.Models.Requirements) on type ATL.Trebuchet.RestApi.Controllers.ValuesController</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace> at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)
at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)
at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)</StackTrace>
</Error>


Please help how can have same Post method with different type of parameters

I am sending data as
Text (text/plain)


EDIT 1 :

enter image description here

Answer Source

If you want to use multiple post method in the same controller you need to map them to different routes or actions

For Web api 1

Add the route to the WebApiConfig, You can look this answer for details but the important thing is to specify that the default route api/controller/id only accepts integers. Else the actions will be treated as string ids.

routes.MapHttpRoute(
    name: "ControllerAndId",
    routeTemplate: "api/{controller}/{id}",
    defaults: null,
    constraints: new { id = @"^\d+$" } // Only integers 
);
routes.MapHttpRoute(
    name: "ActionApi",
    routeTemplate: "api/{controller}/{action}"
);

Then in the controller specify the action ontop of the method

public class DataController : ApiController
{
    [HttpPost]  
    [ActionName("post1")]      
    public HttpResponseMessage Post([StringBody]string data)
    {
       // Logic
    }

    [HttpPost]
    [ActionName("post2")]
    public HttpResponseMessage Post(Requirements objRequirement)
    { 
      //Logic
    }
}

For Web api 2

Here you can use Attribute Routing

[RoutePrefix("api/data")]
public class DataController : ApiController
{
    [HttpPost]  
    [Route("post1")]      
    public HttpResponseMessage Post([StringBody]string data)
    {
       // Logic
    }

    [HttpPost]
    [Route("post2")]
    public HttpResponseMessage Post(Requirements objRequirement)
    { 
      //Logic
    }
}

The first post method will be called as api/data/post1 and the second api/data/post2