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!