ASP .NET MVC2 Changes: GetControllerInstance & DataAnnotationsModelBinder
Posted in aspmvc on March 30th, 2010 by taswar
So was in the process of updating my mvc app, there is a tool that does it for you and also a manual way .
But once you have updated there are also 2 more changes that you may need if you are using IoC or DataAnnotations.
First you will need to change the method in your Ioc to look for controller
1 2 3 4 5 6 7 8 9 10 11 | public class CommonServiceLocatorControllerFactory : DefaultControllerFactory { protected override IController GetControllerInstance(Type controllerType) { if (controllerType == null) base.GetControllerInstance(controllerType); return ServiceLocator.Current.GetInstance(controllerType) as IController; } } |
into, notice the RequestContext in the parameter
1 2 3 4 5 6 7 8 9 10 | public class CommonServiceLocatorControllerFactory : DefaultControllerFactory { protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) { if (controllerType == null) base.GetControllerInstance(requestContext, controllerType); return ServiceLocator.Current.GetInstance(controllerType) as IController; } } |
The other change is in DataAnnotationsModelBinder if you have one, rather than inherit from Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder inherit from DefaultModelBinder
Hope this helps
2 Responses
Leave a Comment

January 19th, 2011 at 4:41 pm
Thanks! I’m using a good book that uses MVC, and I don’t think they have an MVC2 version out. This helped! I had an inkling that I needed the new RequestContext parameter … but Visual Studio gave me an error that said GetControllerInstance does not exist?? Weird.
April 12th, 2012 at 4:16 pm
Thanks for the help, I was working through example by
Tim Ross at
http://timross.wordpress.com/2010/01/21/creating-a-simple-ioc-container/
and got a override error!