c# - Custom ControllerFactory does not replace DefaultControllerFactory when there is a controller collision -
i have custom controller factory (very basic implementation relevant question):
public class mycontrollerfactory : defaultcontrollerfactory { public override icontroller createcontroller(requestcontext requestcontext, string controllername) { var controller = base.createcontroller(requestcontext, controllername); return controller; } protected override system.web.sessionstate.sessionstatebehavior getcontrollersessionbehavior(requestcontext requestcontext, type controllertype) { return base.getcontrollersessionbehavior(requestcontext, controllertype); } }
which registered in global.asax
this:
protected void application_start() { controllerbuilder.current.setcontrollerfactory(typeof(mycontrollerfactory)); // other code here }
breakpoints set in controller factory hit when there no conflict in resolving controller (e.g. 1 controller found in resolution process). however, when have 2 controllers same name (in case: 1 in area, 1 not in area) without using namespaces constraint in route, controller isn't hit, , defaultcontrollerfactory
takes on , throws (expected) exception.
my questions
am registering mycontrollerfactory
properly? there other reason it's not getting used in case i've outlined above?
what i'm trying do
i'm trying write controllerfactory
automatically uses controllers defined in (config-specified) area (i.e. if conflict occurs, above, use area controller instead of non-area controller). if area doesn't contain matching controller, fall 1 not in area.
you try using global namespace prioritization. use add
method of controllerbuilder.current.defaultnamespaces
add namespaces should given higher priority. namespaces added given same priority, doesn't matter order add them in, given higher priority namespaces not added. add config-specified namespace method , should cause search there first. add in application_start
, maybe after line register controller factory:
controllerbuilder.current.defaultnamespaces.add("mycontrollernamespace");
you can add ".*" after namespace name have search child namespaces well.
you directly implement icontrollerfactory
interface instead of extending defaultcontrollerfactory
if default implementation getting in way when there name collision.
Comments
Post a Comment