0

This controller:

public class TestController <T> : Controller
{
    public string Index()
    {             
        return "123";
    }
}

This definition of the routes:

routes.MapRoute(
     "Default",
     "{controller}/{action}/{id}",
     new
     {
          controller = "Test<T>",
          action = "Index",
          id = "" 
     });

This is the error I get:

The IControllerFactory 'Yad2.Web.Mvc.UI.Infrastructure.NinjectControllerFactory' did not return a controller for the name 'Test'.

Jsinh
  • 2,539
  • 1
  • 19
  • 35

1 Answers1

1
public class TestController <T>   : Controller
{
    public string Index( )
    {             
        return "123";
    }
}

List of things wrong here:

  1. Controllers cannot be generic abstract classes, you need to define T.
  2. All actions need to return ActionResult derived objects.
  3. "Test" in your route definition is absolutely incorrect. Never going to work.
  4. You never showed your Ninject registration.
Khalid Abuhakmeh
  • 10,709
  • 10
  • 52
  • 75
  • Agreed with everything but 2. You can return `string` or `bool` from Action Result methods. – krillgar Mar 21 '14 at 18:22
  • There is a way to use a Generic controller.. Check this post: http://stackoverflow.com/questions/848904/in-asp-net-mvc-is-it-possible-to-make-a-generic-controller – Nitin Midha Mar 21 '14 at 18:23
  • @krillgar it isn't advised you do so, if you want to do that use the Content() method in the controller. – Khalid Abuhakmeh Mar 21 '14 at 18:25
  • @NitinMidha The controller in the link you sent me is a generic controller where **T** is defined. It is not defined in the above code. Essentially this user wants to magically pull a generic type out of thin air. You can't do that unless you build a mechanism to set **T** dynamically and create it (maybe using route values). MVC will not resolve **abstract** controllers. – Khalid Abuhakmeh Mar 21 '14 at 18:29