<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Gaute's .net weblog - ASP.NET MVC</title>
    <link>http://gaute.amende.no/</link>
    <description>thoughts on programming and computer related stuff</description>
    <language>en-us</language>
    <copyright>gaute</copyright>
    <lastBuildDate>Fri, 12 Jun 2009 11:18:17 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.1.8102.813</generator>
    <managingEditor>gaute@amende.no</managingEditor>
    <webMaster>gaute@amende.no</webMaster>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=2a040d40-6e4d-4411-b3c4-c57abfaec18a</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=2a040d40-6e4d-4411-b3c4-c57abfaec18a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">This blog post is about changing the DefaultControllerFactory
to a custom one to allow constructor dependency injection. In this post I use <a href="http://structuremap.sourceforge.net/Default.htm">Structuremap</a>, <a href="http://commonservicelocator.codeplex.com/">CommonServiceLocator</a> and
I use a slightly changed version of the <a href="http://commonservicelocator.codeplex.com/Wiki/View.aspx?title=StructureMap%20Adapter">CommonServiceLocator.StructureMapAdatpter</a>. 
<br /><br />
The DefaultControllerFactory needs a constructor without any input parameters. I want
to get rid of dependencies in my code, so I put all my interface dependencies in the
constructor and want to use structuremap to handle this for me. I have used this in
a semi-large project, but in this blog post I will use an example with only one dependency
and only one page.<br /><br />
Lets get started:<br /><br />
I start by creating a new ASP.NET MVC project. I then delete all controllers and views
except for my home controller and my index view.<br /><br />
Then I add a Tools folder and a PersonRepository class in my new folder. This only
has a default constructor and a get method that returns a Person, a simple object
with only a name Property. I also extract an interface from PersonRepository called
IPersonRepository.<br /><br />
In my homecontroller I create a constructor that takes a Ipersonrepository as a parameter.
This is my HomeController now:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Web.Mvc; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> DIControllerFactoryExample.Tools; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Controllers
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> HomeController
: Controller { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">readonly</span> IPersonRepository
personRepository; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> HomeController(IPersonRepository
personRepository) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>.personRepository <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> personRepository;
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> ActionResult
Index() { var personid <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> 1;
ViewData.Model <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> personRepository.Get(personid); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> View();
} } }</span><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><br /></span></pre>I compile, run and get a: "No parameterless constructor defined for this
object." exception. Just as planned.<br />
Now, the fun begins. Getting this to work.<br /><br />
I start by adding a solution folder called Lib, and adding StructureMap.dll and Microsoft.Practices.ServiceLocation.dll
and reference them in the solution. These can be found at the locations in the top
of this blog post.<br /><br />
Then I add 3 new files to my Tools folder.<br />
StructureMapServiceLocator.cs, DefaultStructureMapRegistry.cs, CommonServiceLocatorControllerFactory.cs<br /><br />
These files look like this:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> StructureMap.Configuration.DSL; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Tools
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> DefaultStructureMapRegistry
: Registry { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> DefaultStructureMapRegistry()
{ ForRequestedType&lt;IPersonRepository&gt;().TheDefaultIsConcreteType &lt;PersonRepository&gt;();
} } }<pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Collections.Generic; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> Microsoft.Practices.ServiceLocation; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> StructureMap; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Tools
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> StructureMapServiceLocator
: ServiceLocatorImplBase { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span> IContainer
container; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> StructureMapServiceLocator(IContainer
container) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>.container <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> container;
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">protected</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">override</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> DoGetInstance(Type
serviceType, <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> key)
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span>.IsNullOrEmpty(key)
? container.GetInstance(serviceType) : container.GetInstance(serviceType, key); } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">protected</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">override</span> IEnumerable&lt;<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span>&gt;
DoGetAllInstances(Type serviceType) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">foreach</span> (<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> obj <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">in</span> container.GetAllInstances(serviceType))
{ yield <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> obj;
} } } } </span></pre></span></pre><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Web.Mvc; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> Microsoft.Practices.ServiceLocation; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Tools
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> StructureMapServiceLocatorControllerFactory
: DefaultControllerFactory { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">protected</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">override</span> IController
GetControllerInstance(Type controllerType) { var controller <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> ServiceLocator.Current.GetInstance(controllerType) <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">as</span> Controller; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> controller;
} } } </span></pre><br />
I also add the following to the Application_Start method in my Global.asax.cs:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">var
registry <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> DefaultStructureMapRegistry();
var container <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> Container(registry);
ServiceLocator.SetLocatorProvider(() =&gt; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> StructureMapServiceLocator(container));
var locator <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> StructureMapServiceLocatorControllerFactory();
ControllerBuilder.Current.SetControllerFactory(locator);</span></pre><br />
I compile, reload and everything works. That's is. Good luck;) 
<br /><br />
EDIT: Did a small fix in the code i global.asax, check out this <a href="http://blogs.msdn.com/miah/archive/2009/05/12/servicelocator-and-unity-be-careful.aspx">post</a><br /><br />
The full Visual Studio solution can be downloaded here:<br /><p></p><a href="http://gaute.amende.no/content/binary/DIControllerFactoryExample.zip">DIControllerFactoryExample.zip
(463.5 KB)</a><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=2a040d40-6e4d-4411-b3c4-c57abfaec18a" /></body>
      <title>Changing the ASP.NET MVC DefaultControllerFactory with a custom using StructureMap and Common Service Locator.</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</guid>
      <link>http://gaute.amende.no/ChangingTheASPNETMVCDefaultControllerFactoryWithACustomUsingStructureMapAndCommonServiceLocator.aspx</link>
      <pubDate>Fri, 12 Jun 2009 11:18:17 GMT</pubDate>
      <description>This blog post is about changing the DefaultControllerFactory to a custom one to allow constructor dependency injection. In this post I use &lt;a href="http://structuremap.sourceforge.net/Default.htm"&gt;Structuremap&lt;/a&gt;, &lt;a href="http://commonservicelocator.codeplex.com/"&gt;CommonServiceLocator&lt;/a&gt; and
I use a slightly changed version of the &lt;a href="http://commonservicelocator.codeplex.com/Wiki/View.aspx?title=StructureMap%20Adapter"&gt;CommonServiceLocator.StructureMapAdatpter&lt;/a&gt;. 
&lt;br&gt;
&lt;br&gt;
The DefaultControllerFactory needs a constructor without any input parameters. I want
to get rid of dependencies in my code, so I put all my interface dependencies in the
constructor and want to use structuremap to handle this for me. I have used this in
a semi-large project, but in this blog post I will use an example with only one dependency
and only one page.&lt;br&gt;
&lt;br&gt;
Lets get started:&lt;br&gt;
&lt;br&gt;
I start by creating a new ASP.NET MVC project. I then delete all controllers and views
except for my home controller and my index view.&lt;br&gt;
&lt;br&gt;
Then I add a Tools folder and a PersonRepository class in my new folder. This only
has a default constructor and a get method that returns a Person, a simple object
with only a name Property. I also extract an interface from PersonRepository called
IPersonRepository.&lt;br&gt;
&lt;br&gt;
In my homecontroller I create a constructor that takes a Ipersonrepository as a parameter.
This is my HomeController now:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Web.Mvc; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; DIControllerFactoryExample.Tools; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Controllers
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; HomeController
: Controller { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;readonly&lt;/span&gt; IPersonRepository
personRepository; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; HomeController(IPersonRepository
personRepository) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.personRepository &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; personRepository;
} &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; ActionResult
Index() { var personid &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; 1;
ViewData.Model &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; personRepository.Get(personid); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; View();
} } }&lt;/span&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;I compile, run and get a: "No parameterless constructor defined for this
object." exception. Just as planned.&lt;br&gt;
Now, the fun begins. Getting this to work.&lt;br&gt;
&lt;br&gt;
I start by adding a solution folder called Lib, and adding StructureMap.dll and Microsoft.Practices.ServiceLocation.dll
and reference them in the solution. These can be found at the locations in the top
of this blog post.&lt;br&gt;
&lt;br&gt;
Then I add 3 new files to my Tools folder.&lt;br&gt;
StructureMapServiceLocator.cs, DefaultStructureMapRegistry.cs, CommonServiceLocatorControllerFactory.cs&lt;br&gt;
&lt;br&gt;
These files look like this:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; StructureMap.Configuration.DSL; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Tools
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; DefaultStructureMapRegistry
: Registry { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; DefaultStructureMapRegistry()
{ ForRequestedType&amp;lt;IPersonRepository&amp;gt;().TheDefaultIsConcreteType &amp;lt;PersonRepository&amp;gt;();
} } }&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Collections.Generic; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; Microsoft.Practices.ServiceLocation; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; StructureMap; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Tools
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; StructureMapServiceLocator
: ServiceLocatorImplBase { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; IContainer
container; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; StructureMapServiceLocator(IContainer
container) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.container &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; container;
} &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt; DoGetInstance(Type
serviceType, &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; key)
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt;.IsNullOrEmpty(key)
? container.GetInstance(serviceType) : container.GetInstance(serviceType, key); } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt;&amp;gt;
DoGetAllInstances(Type serviceType) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;foreach&lt;/span&gt; (&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt; obj &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;in&lt;/span&gt; container.GetAllInstances(serviceType))
{ yield &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; obj;
} } } } &lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Web.Mvc; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; Microsoft.Practices.ServiceLocation; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Tools
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; StructureMapServiceLocatorControllerFactory
: DefaultControllerFactory { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; IController
GetControllerInstance(Type controllerType) { var controller &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; ServiceLocator.Current.GetInstance(controllerType) &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;as&lt;/span&gt; Controller; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; controller;
} } } &lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
I also add the following to the Application_Start method in my Global.asax.cs:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;var
registry &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; DefaultStructureMapRegistry();
var container &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Container(registry);
ServiceLocator.SetLocatorProvider(() =&amp;gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; StructureMapServiceLocator(container));
var locator &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; StructureMapServiceLocatorControllerFactory();
ControllerBuilder.Current.SetControllerFactory(locator);&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
I compile, reload and everything works. That's is. Good luck;) 
&lt;br&gt;
&lt;br&gt;
EDIT: Did a small fix in the code i global.asax, check out this &lt;a href="http://blogs.msdn.com/miah/archive/2009/05/12/servicelocator-and-unity-be-careful.aspx"&gt;post&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
The full Visual Studio solution can be downloaded here:&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;a href="http://gaute.amende.no/content/binary/DIControllerFactoryExample.zip"&gt;DIControllerFactoryExample.zip
(463.5 KB)&lt;/a&gt;&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=2a040d40-6e4d-4411-b3c4-c57abfaec18a" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>ASP.NET MVC</category>
      <category>Patterns</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=f77906f7-da3e-4a40-9742-572aa24db274</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,f77906f7-da3e-4a40-9742-572aa24db274.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,f77906f7-da3e-4a40-9742-572aa24db274.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=f77906f7-da3e-4a40-9742-572aa24db274</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">I have recently been working on a project
where we reimplement an old web application with a new one. Since there has been an
page on the same address before, there are also some old applications in other places
linking to pages on the old site. For instance, the login.aspx file is linked to in
a lot of places. 
<br /><br />
I will in my example use a link to the page login.aspx in my solution.<br /><br />
To get these links to work, I had (as far as I'm aware of) two options. To make a
login.aspx and redirect in this file and add login.aspx to global.asax IgnoreRoute,
or to map a new route in global.asax and redirect to my new login. The latter is the
solution i went for.<br /><br />
To accomplish this all I had to do was to add the following to RegisterRoutes method
in global asax:<br /><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><br />
routes.MapRoute(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Handle
login"</span>, <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"login.aspx"</span>, <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> {
controller <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Login"</span>,
action <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Index"</span>,
id <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">""</span> }); </span><br /><br />
Very easy, and now, every request for http://myapp/login.aspx is handled by http://myapp/login/index/<br /><p></p><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=f77906f7-da3e-4a40-9742-572aa24db274" /></body>
      <title>Adding a custom route to ASP.NET MVC to allow old .aspx urls to work.</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,f77906f7-da3e-4a40-9742-572aa24db274.aspx</guid>
      <link>http://gaute.amende.no/AddingACustomRouteToASPNETMVCToAllowOldAspxUrlsToWork.aspx</link>
      <pubDate>Fri, 05 Jun 2009 16:43:20 GMT</pubDate>
      <description>I have recently been working on a project where we reimplement an old web application with a new one. Since there has been an page on the same address before, there are also some old applications in other places linking to pages on the old site. For instance, the login.aspx file is linked to in a lot of places. &lt;br&gt;
&lt;br&gt;
I will in my example use a link to the page login.aspx in my solution.&lt;br&gt;
&lt;br&gt;
To get these links to work, I had (as far as I'm aware of) two options. To make a
login.aspx and redirect in this file and add login.aspx to global.asax IgnoreRoute,
or to map a new route in global.asax and redirect to my new login. The latter is the
solution i went for.&lt;br&gt;
&lt;br&gt;
To accomplish this all I had to do was to add the following to RegisterRoutes method
in global asax:&lt;br&gt;
&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
routes.MapRoute(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Handle
login"&lt;/span&gt;, &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"login.aspx"&lt;/span&gt;, &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; {
controller &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Login"&lt;/span&gt;,
action &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Index"&lt;/span&gt;,
id &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;""&lt;/span&gt; }); &lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
Very easy, and now, every request for http://myapp/login.aspx is handled by http://myapp/login/index/&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=f77906f7-da3e-4a40-9742-572aa24db274" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,f77906f7-da3e-4a40-9742-572aa24db274.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>ASP.NET MVC</category>
    </item>
  </channel>
</rss>