[ASP.NET MVC ] Controller Factory

June 4, 2012 at 6:29 pm Leave a comment

The controller factory is responsible for creating instances of controllers in a request pipeline. Usually you might be content with the MVC’s default implementation but you might be interested in how to configure this to control its behavior.

1. IControllerFactory

The base interface is always a good starting point. This time, it’s “System.Web.Mvc.IControllerFactory“.

public interface IControllerFactory
{
  IController CreateController(RequestContext requestContext, string controllerName);
  SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName);
  void ReleaseController(IController controller);
}

The most important method is “CreateController()”. The routing system determines the name of a controller and passes it to the “CreateController()” method along with request information.

2. DefaultControllerFactory

The “System.Web.Mvc.DefaultControllerFactory” class is a default controller factory implementation for MVC.

The default implementation is like this: It tries to find the class with the name that matches with the “controller” parameter in route values. The controller class should meet the following criteria:

  • The class must be public.
  • The class must be not abstract
  • The class must implement the “IController” interface
  • The name of the class must end with “Controller
  • The class must not take generic parameters

The “DefaultControllerFactory” class define the “GetControllerInstance()” method to retrieve an controller instance.

protected internal virtual IController GetControllerInstance(RequestContext requestContext, Type controllerType)

3. Creating a Custom Controller Factory

You can build a controller factory from scratch by implementing the “IControllerFactory” interface. But sometimes it is easier to inherit from the “DefaultControllerFactory“.

4. Registering a Custom Controller Factory

The “System.Web.Mvc.ControllerBuilder” class manages how the controller can be created. You can get an instance of this class using the static “Current” property.

  • public static ControllerBuilder Current { get; }

Once you get an instance of the “ControllerBuilder” class, you can use the following methods to get or set a contoller factory.

  • public IControllerFactory GetControllerFactory()
  • public void SetControllerFactory(Type controllerFactoryType)
  • public void SetControllerFactory(IControllerFactory controllerFactory)

This task is usually done in the “Application_Start()” method in the “Global.asax“.

protected void Application_Start()
{
  ...
  ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
}
About these ads

Entry filed under: ASP.NET, MVC. Tags: , .

[ASP.NET MVC] Filters – Exception [ASP.NET MVC ] Controller Factory and Ninject

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


About

As a web developer, I am happy to have a place to share my little know-hows regarding web-development.

MCSD

Recent Posts

Categories


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: