Here are some changes that affected my asp mvc update process, specifically ASP .NET MVC2 Changes: GetControllerInstance & DataAnnotationsModelBinder.
By the way, 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
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
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 🙂
View Comments (2)
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.
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!