1

Is it possible to look at the body of the http method and the fact it is POST (verb) call a different method on the same controller?

So I need something like this:

void
[HttpPost]
public HttpResponseMessage DeleteItem(DeleteItemCommand deleteItemCommand)
{
   return null;
}

[HttpPost] 
public HttpResponseMessage UpdateItem(UpdateItemCommand updateItemCommand)
{
   return null;
}

and this all works with a single URL : [Route("api/controller")].

So in Javascript my call will have in the body something like:

{
    DeleteItemCommand : { 
       etc.
    }
}

or even use a HTTP header like domain-model="DeleteItemCommand".

JD.
  • 15,171
  • 21
  • 86
  • 159
  • Yes but you have to give each method its own named route. – Igor Feb 15 '18 at 14:56
  • You should use DELETE (HttpDelete) for delete operations and PUT (HttpPut) for update operations. In that way the routes will be a no issue – Marcus Höglund Feb 15 '18 at 15:01
  • Sorry maybe delete was not a good example..the point is I do not want to use different routes. Is it possible? – JD. Feb 15 '18 at 15:03
  • Yes by using different HTTP methods, as suggested by Marcus. otherwise no - how would the system know which of the overloaded action methods was intended if the route is identical?? Don't forget it cannot use the type of the object in the method call to determine it, as it might with a C#->C# method call, because the object doesn't exist yet. It has to choose which action method to call, and _then_ try to deserialise the submitted form fields into that type of object. Consequently it has no means of distinguishing which action method to use. – ADyson Feb 15 '18 at 16:04
  • And before you ask - no it can't check the body, attempt to deserialise to all the possible types (a method can be overloaded infinitely in theory, don't forget) and make a decision that way because a) it would be quite an expensive operation and b) deserialisation doesn't require an exact mapping to the target object - there can be missing fields, and fields not matching the target type's parameters which simply get discarded, so it can't be used to prove that the target type is the correct one. – ADyson Feb 15 '18 at 16:06
  • @ADyson: Thanks for the help. Can you explain how it is done here: https://github.com/aliostad/m-r/blob/master/SimpleCQRS.Api/Controllers/InventoryItemController.cs – JD. Feb 16 '18 at 00:24
  • Does that code actually work? – ADyson Feb 16 '18 at 07:49
  • Yep, I pulled it down and compiled it and set debug points. Seems to hit all of them. – JD. Mar 06 '18 at 17:43

0 Answers0