X

ASP .NET MVC2 Changes: GetControllerInstance & DataAnnotationsModelBinder

ASP.NET-MVC

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 🙂

Categories: aspmvc
Taswar Bhatti:

View Comments (2)

Related Post