Question

Is it possible to map a route with MapRoute and specify a generic controller e.g

        context.MapRoute(
            "Dashboard_Edit", // Route name
            "dashboard/edit/{*pagePath}",
            new { controller = "Dashboard`1", action = "edit", pagePath = "home" }
            );
Was it helpful?

Solution

It is unfortunately not allowed with the default controller factory. The type "Dashboard`1" is for an open generic type and cannot be constructed. In other words, with the default controller factory the only allowed values for "controller" are ones that can fit the following pseudo syntax:

IController c = new SomeControllerType();

The SomeControllerType must be valid (though without the "Controller" suffix or namespace), and it must have a parameterless constructor.

You could always write a custom controller factory that has more advanced functionality and understands how to construct generic types.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow