Is it possible to have a generic method in a controller? I'm talking about something like this:
[HttpPost]
public void DoSomething<T>([FromBody] SomeGenericClass<T> someGenericObject)
{
SomePrivateMethod<T>(someGenericObject);
}
I have actually tried the above (though with different names for everything) and posted to Api/<controllername>/DoSomething
with the instance of someGenericObject<T>
in the request body, and it didn't work (i.e. it didn't reach the controller).
I'm guessing that Web API routing is not able to resolve generic methods since they may result in different methods for different types underneath. But that's just what I think.
So, is it possible to have a generic method in a controller?
- If yes, how?
- If not, why?