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!

ca8msm
  • 1,170
  • 3
  • 15
  • 37
  • are you overriding the default controller factory method? If not, will the default controller factory be able to create an instance for this controller? – Developer Dec 23 '16 at 09:44
  • No I'm not, is that what you think I should be doing? – ca8msm Dec 23 '16 at 09:45
  • Could you please give more info on why you need your controller to be generic? What is that you have in mind? – Developer Dec 23 '16 at 09:50
  • How do you expect to provide the `` 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`? – Peter B Dec 23 '16 at 09: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. – ca8msm Dec 23 '16 at 09:54
  • If I understood you correctly, you will not be having `DepartmentsController`, instead, you will have `GenericController` which should resolve as `GenericController` for `Department` entity - In this case, definitely you gotta go for your own implementation of controller factory. – Developer Dec 23 '16 at 09:59
  • Possible duplicate of [In asp.net mvc is it possible to make a generic controller?](http://stackoverflow.com/questions/848904/in-asp-net-mvc-is-it-possible-to-make-a-generic-controller) – Developer Dec 23 '16 at 10:01
  • @Peter Yes, that makes sense. I've changed it to BaseController however I still get the same issue. – ca8msm Dec 23 '16 at 10:14
  • Possible duplicate of [asp.net mvc generic controller](http://stackoverflow.com/questions/3413626/asp-net-mvc-generic-controller) – Peter B Dec 23 '16 at 10:37

0 Answers0