1

I have a controller which inherits from a base controller and the default routing:

 routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

works correctly when going to /Departments/Create:

public class DepartmentsController : BaseController
{

    public ActionResult Create()
    {
        return View("Create");
    }

public abstract class BaseController : Controller
{
....

However if I try and change this to be a generic controller e.g.

public class DepartmentsController<T> : BaseController<T>
    where T: class
{

    public ActionResult Create()
    {
        return View("Create");
    }

public abstract class BaseController<T> : Controller
    where T: class
{
....

Then going to /Departments/Create I end up with a "The resource cannot be found" error suggesting that the action has not been found. However, when I check the routes used with routeDebugger I can see that "{controller}/{action}/{id}" has been matched yet the action has not been called:

MVC Route

Is there a different route or method I would have to use to be able to call the correct action?

Thanks!

9
  • are you overriding the default controller factory method? If not, will the default controller factory be able to create an instance for this controller? Commented Dec 23, 2016 at 9:44
  • No I'm not, is that what you think I should be doing? Commented Dec 23, 2016 at 9:45
  • Could you please give more info on why you need your controller to be generic? What is that you have in mind? Commented Dec 23, 2016 at 9:50
  • How do you expect to provide the <T> to MVC when calling /Departments/Create? There is nothing in the URL that specifies T, so how could MVC know. Maybe you meant to use public class DepartmentsController : BaseController<Department>? Commented Dec 23, 2016 at 9:53
  • Because all of my entities will follow a defined interface and will all have the same named methods that can be called. Rather than creating a controller for each entity that has the exact same code (but with the entity name changed) I'd like to create one base controller that has all of the actions that are common across each entity and then each entity only needs to add actions that are specific to that particular entity. Commented Dec 23, 2016 at 9:54

0

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.