Jörg Zeyn
Jörg Zeyn

Reputation: 81

How to handle two different HttpPost in one controller

I've got a generic controller with

 [HttpPost]
 public void Create(T entity)
 {
    ...
 }

and an extra controller wich inherits from the generic controller and has got this method in it:

[ActionName("AddPrivileges")]
public void AddPrivileges(AddPrivilegeModel model)
{
    ...
}

My problem now is, that the controller has two HttpPost requests in it. I tried to fix it with routing, but I think I did something terrible wrong.

config.Routes.MapHttpRoute(
    name: "RoleActionRoute",
    routeTemplate: "api/Role2/AddPrivileges"
);

What can or should I do?

Upvotes: 1

Views: 142

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123901

You can provide the special mapping like this

config.Routes.MapHttpRoute(
    name: "SpecialAction",
    routeTemplate: "api/{controller}/AddPrivileges",
    defaults: new { action = "AddPrivileges" }
    , constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
    );

config.Routes.MapHttpRoute(
    name: "PostAction",
    routeTemplate: "api/{controller}",
    defaults: new { action = "Create" }
    , constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
    );
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
    );

So, your special method AddPrivileges has now special mapping

Upvotes: 2

Related Questions