3

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

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

8 Comments

Not supporting routePrefix .. its telling me to add new class.. see edit1
Generate class for Route : same thing for [Route("")]
sorry, Didnt see you were using web api 1. In web api 2 you can use attribute routing which my solution is based on
Is there any solution for Web api 1 ??
same error with url :- localhost:57578/api/Values/PostString Note:- PostString :- ActionName > [ActionName("PostString")]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.