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'.

2
  • How come you are using a generic controller? I've never seen that before. Commented Mar 21, 2014 at 18:21
  • 1
    Generic controller??? Show how are you configuring Ninject. Commented Mar 21, 2014 at 18:21

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

4 Comments

Agreed with everything but 2. You can return string or bool from Action Result methods.
There is a way to use a Generic controller.. Check this post: stackoverflow.com/questions/848904/…
@krillgar it isn't advised you do so, if you want to do that use the Content() method in the controller.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.