Multiple Httppost Method in Web API Controller

Multiple HttpPost method in Web API controller

You can have multiple actions in a single controller.

For that you have to do the following two things.

  • First decorate actions with ActionName attribute like

     [ActionName("route")]
    public class VTRoutingController : ApiController
    {
    [ActionName("route")]
    public MyResult PostRoute(MyRequestTemplate routingRequestTemplate)
    {
    return null;
    }

    [ActionName("tspRoute")]
    public MyResult PostTSPRoute(MyRequestTemplate routingRequestTemplate)
    {
    return null;
    }
    }
  • Second define the following routes in WebApiConfig file.

    // Controller Only
    // To handle routes like `/api/VTRouting`
    config.Routes.MapHttpRoute(
    name: "ControllerOnly",
    routeTemplate: "api/{controller}"
    );


    // Controller with ID
    // To handle routes like `/api/VTRouting/1`
    config.Routes.MapHttpRoute(
    name: "ControllerAndId",
    routeTemplate: "api/{controller}/{id}",
    defaults: null,
    constraints: new { id = @"^\d+$" } // Only integers
    );

    // Controllers with Actions
    // To handle routes like `/api/VTRouting/route`
    config.Routes.MapHttpRoute(
    name: "ControllerAndAction",
    routeTemplate: "api/{controller}/{action}"
    );

Multiple HttpPost action with the same parameter in Web API Controller

The order in which you register your routes is important. register more specific routes first and the more general routes after.

public static void Register(HttpConfiguration config) {
// Web API configuration and services

// Web API routes
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: "ControllerAndAction",
routeTemplate: "api/{controller}/{action}/{id}"
defaults: new { id = RouteParameter.Optional }
);

config.Routes.MapHttpRoute(
name: "ControllerandId",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

}

You should also update the controller to be a little more specific as to what it can handle.

public class RegionManagementController : ApiController {
[HttpPost]
[ActionName("AddNewState")]
public object PostAddNewState(RegionInformation newStateParam) { ... }

[HttpPost]
[ActionName("AddNewCity")]
public object PostAddNewCity(RegionInformation newCityParam) { ... }
}

web api multiple post methods with different parameter type

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

ASP.NET Web API - Multiple POST methods on one controller?

You have to include the action in your route:

routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);


Related Topics



Leave a reply



Submit