<?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</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>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=6b0672a0-1adf-4322-a84a-8a646686779c</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,6b0672a0-1adf-4322-a84a-8a646686779c.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,6b0672a0-1adf-4322-a84a-8a646686779c.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=6b0672a0-1adf-4322-a84a-8a646686779c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Today I got an error on a web application
after upgrading it from .Net 2.0 and Visual Studio 2005 to .Net 3.5. The project uses
Telerik radcontrol, a few releases old version. After upgrading, every page that uses
the radcontrol DatePicker failed to load, and the dreaded "Yellow screen of death"
was shown instead.<br /><br />
It wasen't my code that failed, it was the radcontrols own prerender-event that threw
the exception. Since I don't have the source code for this control and due to the
fact that the fault only appaired on one test server without Visual Studio installed,
it was hard to debug. Luckily a colleague had experienced a similair problem some
weeks ago, and I remembered that changing the locale settings helped him (He is swedish,
and used a swedish locale in an otherwise homogenic norwegian development team).<br /><br />
I logged in to the server and tried to change the system locale of the admin user.
No help.<br />
I tried to change the user running the application pool from NETWORK SERVICE to the
admin I logged in as. Suddenly everything worked.<br /><br />
I did however want to run the process as NETWORK SERVICE and not administrator or
have to make another account just for this reason. Therefore I wanted to change the
network services locale settings, but since this is a user you can't log in as, this
seemed hard.<br /><br />
Luckily I found this blog post: <a href="http://www.request-response.com/blog/PermaLink,guid,e6ba0263-8825-4a6d-91e8-f5ce7f92111e.aspx">IIS6:
Changing the Locale ID when Regional Settings Won't Work</a><br />
I followed the description to change the settings. For some reason this didn't quite
work. Probably due to a typo. I tried to export the regedit settings for HKEY_USERS\S-1-5-20
and just manually change the sShortDate, from the English setting and to norwegian
settings. Now everything is working again!<p></p><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=6b0672a0-1adf-4322-a84a-8a646686779c" /></body>
      <title>"String was not recognized as a valid DateTime" from Telerik Calendar Datepicker</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,6b0672a0-1adf-4322-a84a-8a646686779c.aspx</guid>
      <link>http://gaute.amende.no/StringWasNotRecognizedAsAValidDateTimeFromTelerikCalendarDatepicker.aspx</link>
      <pubDate>Tue, 28 Oct 2008 20:20:13 GMT</pubDate>
      <description>Today I got an error on a web application after upgrading it from .Net 2.0 and Visual Studio 2005 to .Net 3.5. The project uses Telerik radcontrol, a few releases old version. After upgrading, every page that uses the radcontrol DatePicker failed to load, and the dreaded "Yellow screen of death" was shown instead.&lt;br&gt;
&lt;br&gt;
It wasen't my code that failed, it was the radcontrols own prerender-event that threw
the exception. Since I don't have the source code for this control and due to the
fact that the fault only appaired on one test server without Visual Studio installed,
it was hard to debug. Luckily a colleague had experienced a similair problem some
weeks ago, and I remembered that changing the locale settings helped him (He is swedish,
and used a swedish locale in an otherwise homogenic norwegian development team).&lt;br&gt;
&lt;br&gt;
I logged in to the server and tried to change the system locale of the admin user.
No help.&lt;br&gt;
I tried to change the user running the application pool from NETWORK SERVICE to the
admin I logged in as. Suddenly everything worked.&lt;br&gt;
&lt;br&gt;
I did however want to run the process as NETWORK SERVICE and not administrator or
have to make another account just for this reason. Therefore I wanted to change the
network services locale settings, but since this is a user you can't log in as, this
seemed hard.&lt;br&gt;
&lt;br&gt;
Luckily I found this blog post: &lt;a href="http://www.request-response.com/blog/PermaLink,guid,e6ba0263-8825-4a6d-91e8-f5ce7f92111e.aspx"&gt;IIS6:
Changing the Locale ID when Regional Settings Won't Work&lt;/a&gt;
&lt;br&gt;
I followed the description to change the settings. For some reason this didn't quite
work. Probably due to a typo. I tried to export the regedit settings for HKEY_USERS\S-1-5-20
and just manually change the sShortDate, from the English setting and to norwegian
settings. Now everything is working again!&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=6b0672a0-1adf-4322-a84a-8a646686779c" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,6b0672a0-1adf-4322-a84a-8a646686779c.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=2916dc2c-5419-4415-9cbb-daae00fc1785</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,2916dc2c-5419-4415-9cbb-daae00fc1785.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,2916dc2c-5419-4415-9cbb-daae00fc1785.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=2916dc2c-5419-4415-9cbb-daae00fc1785</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
All most every ASP.NET website I have seen, uses Databinder.Eval(Container.DataItem,
"colum1") in repeaters to get the data passed in and show it on screen. The problem
with Databinder.Eval is that it uses reflection to evaluate the item. This is time
consuming and unnecessary, so instead, one should use casting to obtain the same effect.
Try not to do it this way:
</p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
        </p>
        <pre>
          <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">&lt;asp:repeater
id=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"rptAccounts"</span> runat=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"server"</span>&gt;
    &lt;itemtemplate&gt;         &lt;%#
Databinder.Eval(Container.DataItem, <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"colum1"</span>)
%&gt; &lt;br /&gt;     &lt;/itemtemplate&gt; &lt;/asp:repeater&gt; </span>
        </pre>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
But use this one instead:
</p>
        <pre>
          <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">&lt;asp:repeater
id=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"rptAccounts"</span> runat=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"server"</span>&gt;<br />
    &lt;itemtemplate&gt;<br />
        &lt;%# ((BusinessObjects.Object)Container.DataItem).Column1
%&gt; &lt;br /&gt;<br />
    &lt;/itemtemplate&gt;<br />
&lt;/asp:repeater&gt;<br /><br /></span>
          <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
This will also give you a compiler warning if the members of a class should change,
and<br />
not just runtime fail such as the first example. If you are not using business<br />
objects, but a DataTable, you can<span style="">  </span>cast<br />
to datarow and get the column right out of there like this:<span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><br /></span></p>
          <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
            <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">
              <br />
            </span>
          </p>
          <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
            <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">&lt;asp:repeater
id=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"rptAccounts"</span> runat=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"server"</span>&gt;
    &lt;itemtemplate&gt;         &lt;%#
((DataRow)Container.DataItem)[<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Column1"</span>]
%&gt; &lt;br /&gt;     &lt;/itemtemplate&gt; &lt;/asp:repeater&gt; </span>
          </p>
          <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
This both increases speed and makes the application more robust to runtime errors.
A win-win situation:) 
</p>
          <br />
          <br />
        </pre>
        <p>
        </p>
        <p>
        </p>
        <img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=2916dc2c-5419-4415-9cbb-daae00fc1785" />
      </body>
      <title>Stay away from DataBinder.Eval, use explicit  casting</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,2916dc2c-5419-4415-9cbb-daae00fc1785.aspx</guid>
      <link>http://gaute.amende.no/StayAwayFromDataBinderEvalUseExplicitCasting.aspx</link>
      <pubDate>Wed, 03 Sep 2008 18:42:36 GMT</pubDate>
      <description>

&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
All most every ASP.NET website I have seen, uses Databinder.Eval(Container.DataItem,
"colum1") in repeaters to get the data passed in and show it on screen. The problem
with Databinder.Eval is that it uses reflection to evaluate the item. This is time
consuming and unnecessary, so instead, one should use casting to obtain the same effect.
Try not to do it this way:
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&amp;lt;asp:repeater
id=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"rptAccounts"&lt;/span&gt; runat=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"server"&lt;/span&gt;&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;itemtemplate&amp;gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;%#
Databinder.Eval(Container.DataItem, &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"colum1"&lt;/span&gt;)
%&amp;gt; &amp;lt;br /&amp;gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/itemtemplate&amp;gt; &amp;lt;/asp:repeater&amp;gt; &lt;/span&gt;&lt;/pre&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
But use this one instead:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&amp;lt;asp:repeater
id=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"rptAccounts"&lt;/span&gt; runat=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"server"&lt;/span&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;itemtemplate&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;%# ((BusinessObjects.Object)Container.DataItem).Column1
%&amp;gt; &amp;lt;br /&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/itemtemplate&amp;gt;&lt;br&gt;
&amp;lt;/asp:repeater&amp;gt;&lt;br&gt;
&lt;br&gt;
&lt;/span&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
This will also give you a compiler warning if the members of a class should change,
and&lt;br&gt;
not just runtime fail such as the first example. If you are not using business&lt;br&gt;
objects, but a DataTable, you can&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;cast&lt;br&gt;
to datarow and get the column right out of there like this:&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&amp;lt;asp:repeater
id=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"rptAccounts"&lt;/span&gt; runat=&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"server"&lt;/span&gt;&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;itemtemplate&amp;gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;%#
((DataRow)Container.DataItem)[&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Column1"&lt;/span&gt;]
%&amp;gt; &amp;lt;br /&amp;gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/itemtemplate&amp;gt; &amp;lt;/asp:repeater&amp;gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
This both increases speed and makes the application more robust to runtime errors.
A win-win situation:) 
&lt;/p&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=2916dc2c-5419-4415-9cbb-daae00fc1785" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,2916dc2c-5419-4415-9cbb-daae00fc1785.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=c1331ddc-01e5-42d7-b0d7-4c9bbc0d2af7</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,c1331ddc-01e5-42d7-b0d7-4c9bbc0d2af7.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,c1331ddc-01e5-42d7-b0d7-4c9bbc0d2af7.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=c1331ddc-01e5-42d7-b0d7-4c9bbc0d2af7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">I have been struggling a couple of hours
to get the calendar control in .Net to meet my requirements. What I needed was a control
to select a date, so a DateTimePicker would be great. This does not exist in the ASP
framework, so I needed to use the calendar control or buy some third party tool.<br /><br />
My requirements was that only dates within a given time frame should be clickable.
The next and previous month button should also only be clickable if there were clickable
dates in these months. The solution came to me in a dream, no actually a colleague
got me on the idea. The answer was in the events.<br /><br />
Heres the code:<br /><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;">private</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> calDueDate_DayRender(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> sender,
System.Web.UI.WebControls.DayRenderEventArgs e) { DateTime dt <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> Convert.ToDateTime(
dueDate ); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">if</span>(
dt &lt;= DateTime.Today.AddDays(30) ){ e.Day.IsSelectable <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;">false</span>;
} } <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;">void</span> calDueDate_VisibleMonthChanged(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> sender,
System.Web.UI.WebControls.MonthChangedEventArgs e) { DateTime dt <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> Convert.ToDateTime(
pageTask.DueDate); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">if</span>(
dt &lt;= DateTime.Today){ calDueDate.NextMonthText= <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">""</span> calDueDate.NextMonthText= <span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"&gt;"</span>;
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">else</span>{
calDueDate.NextMonthText <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>;
calDueDate.PrevMonthText <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;">"&lt;"</span>;
} } <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;">void</span> calDueDate_Load(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> sender,
System.EventArgs e) { calDueDate.PrevMonthText <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></pre>I must say, the AJAX Calendar looks pretty sweet as well: <a href="http://ajax.asp.net/ajaxtoolkit/Calendar/Calendar.aspx">http://ajax.asp.net/ajaxtoolkit/Calendar/Calendar.aspx</a><p></p><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=c1331ddc-01e5-42d7-b0d7-4c9bbc0d2af7" /></body>
      <title>Calendar control</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,c1331ddc-01e5-42d7-b0d7-4c9bbc0d2af7.aspx</guid>
      <link>http://gaute.amende.no/CalendarControl.aspx</link>
      <pubDate>Fri, 25 Jul 2008 16:20:21 GMT</pubDate>
      <description>I have been struggling a couple of hours to get the calendar control in
.Net to meet my requirements. What I needed was a control to select a
date, so a DateTimePicker would be great. This does not exist in the
ASP framework, so I needed to use the calendar control or buy some
third party tool.&lt;br&gt;
&lt;br&gt;
My requirements was that only dates within a given time frame should be clickable.
The next and previous month button should also only be clickable if there were clickable
dates in these months. The solution came to me in a dream, no actually a colleague
got me on the idea. The answer was in the events.&lt;br&gt;
&lt;br&gt;
Heres the code:&lt;br&gt;
&lt;br&gt;
&amp;nbsp; &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;private&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; calDueDate_DayRender(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt; sender,
System.Web.UI.WebControls.DayRenderEventArgs e) { DateTime dt &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; Convert.ToDateTime(
dueDate ); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt;(
dt &amp;lt;= DateTime.Today.AddDays(30) ){ e.Day.IsSelectable &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;false&lt;/span&gt;;
} } &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;void&lt;/span&gt; calDueDate_VisibleMonthChanged(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt; sender,
System.Web.UI.WebControls.MonthChangedEventArgs e) { DateTime dt &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; Convert.ToDateTime(
pageTask.DueDate); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt;(
dt &amp;lt;= DateTime.Today){ calDueDate.NextMonthText= &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; calDueDate.NextMonthText= &lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"&amp;gt;"&lt;/span&gt;;
} &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;else&lt;/span&gt;{
calDueDate.NextMonthText &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;;
calDueDate.PrevMonthText &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;"&amp;lt;"&lt;/span&gt;;
} } &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;void&lt;/span&gt; calDueDate_Load(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt; sender,
System.EventArgs e) { calDueDate.PrevMonthText &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;/pre&gt;I must say, the AJAX Calendar looks pretty sweet as well: &lt;a href="http://ajax.asp.net/ajaxtoolkit/Calendar/Calendar.aspx"&gt;http://ajax.asp.net/ajaxtoolkit/Calendar/Calendar.aspx&lt;/a&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=c1331ddc-01e5-42d7-b0d7-4c9bbc0d2af7" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,c1331ddc-01e5-42d7-b0d7-4c9bbc0d2af7.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
    </item>
  </channel>
</rss>