2

Is it legal to have controller action methods that are generic? For example, I made this:

[Route("api/[controller]")]
public class NumbersController : Controller
{
    [HttpPost("{orderType}")]
    public async Task<IActionResult> Create<T>(
        [FromBody]
        [ModelBinder(BinderType = typeof(OrderModelBinder))] 
        Order<T> order) where T : IOrder
    {
       //....
    }

My custom OrderModelBinder checks the {orderType} path segment to determine the concrete IOrder used to create the Order<T>.

When I run the code, I get a 404. I am not sure if it is because I've done something wrong, or if ASP.NET simply does not support this.

Perhaps instead of a custom model binder, I should use a custom IActionInvoker?

I'm working with both ASP.NET Core and ASP.NET Web API 2.x. I suspect if this can be done at all, it will be a similar technique either way.

Brent Arias
  • 29,277
  • 40
  • 133
  • 234
  • Look at [this](http://stackoverflow.com/questions/848904/in-asp-net-mvc-is-it-possible-to-make-a-generic-controller) Generic Controller SO question. – Jeroen Heier Apr 16 '16 at 05:16
  • @JeroenHeier Thank you for the link, but an `IControllerFactory` won't help in this case because it is my action-method that is generic, not the controller (and I don't want the controller itself to be generic). – Brent Arias Apr 16 '16 at 06:31

0 Answers0