Can I have multiple POST methods in a web API with various complex parameter types?

I am new to web API ... Here is my main route:

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

Here is what I would like to have:

 public HttpResponseMessage Post( [FromBody] FolderModel theModel ) public HttpResponseMessage Post( [FromBody] DocumentModel theModel ) 

But, the Web API does not find my second Post method. I searched a lot here and on Google, but found nothing for me (well). I know that I could add a second unused parameter to the second method - but this is too big a problem. If this were normal C # code, the compiler would not have problems knowing that to select b / c methods have different signatures. But the web API is not smart enough.

I looked at user restrictions, but that did not seem appropriate. I also can not use different {actions}, since it violates the RESTful restrictions (without RPC, only resources) for my API. I also cannot put the second post on another controller.

The only way I worked with is to wrap both FolderModel and DocumentModel in the parent object as follows:

 public class WorkitemCreateModel { public DocumentModel Document { get; set; } public FolderModel Folder { get; set; } } public HttpResponseMessage Post( [FromBody] WorkitemCreateModel theModel ) 

Then you have one Post method that accepts a WorkitemCreateModel. But then the responsibility of the developer using my API should be transferred to the WorkitemCreateModel, but they should pass only in the DocumentModel object or in the FolderModel object. This is too annoying b / c that my GET API may return a DocumentModel or a FolderModel. So, it would be nice to just pass the object you get from GET to POST. But this does not work, and they must first transfer it to the WorkitemCreateModel object.

Any other suggestions?

By the way: this site is the best! I have found many answers here!

+6
source share
2 answers

It may be an old post, but I am adding this information just in case for people like me who came here looking for answers.

The short answer is NO, it is impossible .

The problem is how routing works, especially in terms of choosing an action method. Here is an excerpt from an ASP.NET article ( https://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection )

The purpose of the selection algorithm is to select an action from a static description before invoking any bindings. Therefore, complex algorithms are excluded from the matching algorithm.

Thus, when comparing action methods with this path, the web API ignores all complex types in the parameter list for this method, and when you do this, both of your methods have 0 parameters and why you encountered this problem.

Hope this helps ...

+5
source

Have you considered changing the method names and trying to use the [HttpPost] attribute?

Source: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

This may answer your question: Several HttpPost methods in a web API controller

+2
source

Source: https://habr.com/ru/post/984673/


All Articles