What's new? | Help | Directory | Sign in
Google
autofac
An addictive .NET IoC container
  
  
  
  
    
Search
for
Updated Mar 17, 2008 by nicholas.blumhardt
MvcIntegration  
Autofac.Integration.Mvc provides an IControllerFactory implementation for System.Web.Mvc

Note: This documentation is for the MIX '08 ASP.NET MVC implementation ('Preview 2'.)

Getting the Code

To use System.Web.Mvc, AKA ASP.NET MVC, AKA MS-MVC, you need to install the CTP from http://asp.net/downloads/3.5-extensions/.

Project Integration

Your ASP.NET MVC web application must reference Autofac.dll, Autofac.Integration.Web.dll and Autofac.Integration.Web.Mvc.dll.

The application must include the ContainerDisposal HTTP module and IContainerProvider implementation in Global.asax that are described on the ASP.NET integration page.

Registering Controllers

You can register controllers by hand, using named registrations:

builder.Register<HomeController>().Named("home").ContainerScoped();

The Autofac integration will convert all controller names to lowercase before retrieving them from the container (you can customise this through IControllerIdentificationStrategy.)

An easier way to register controllers is using AutofacControllerModule, which searches the provided assemblies for implementations of IController:

builder.RegisterModule(new AutofacControllerModule(Assembly.GetExecutingAssembly()));

Installing into ControllerBuilder

The AutofacControllerFactory class ties the container provider into the ASP.NET MVC ControllerBuilder (in the Application_Start() method of the app class):

ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(_containerProvider));

Comment by jake.net, Jun 01, 2008

Just wondering how hard it would be to update this to Preview 3 of asp.net mvc


Sign in to add a comment