Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with custom controller factory

I've recently added Microsoft Unity to my MVC3 project and now I'm getting this error:

The controller for path '/favicon.ico' could not be found or it does not implement IController.

I do not really have a favicon.ico so I have no idea where that's coming from. And the weirdest thing is that the view is actually being rendered and THEN this error is being thrown... I am not sure if it's something wrong with my controller factory class because I got the code from some tutorial (I'm not to IoC - this is the first time I do that). Here's the code:

public class UnityControllerFactory : DefaultControllerFactory { IUnityContainer container;

public UnityControllerFactory(IUnityContainer _container)
{
    container = _container;
}

protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
    IController controller;

    if(controllerType == null)
        throw new HttpException(404, string.Format("The controller for path '{0}' could not be found or it does not implement IController.",
            requestContext.HttpContext.Request.Path));

    if(!typeof(IController).IsAssignableFrom(controllerType))
        throw new ArgumentException(string.Format("Type requested is not a controller: {0}",
                                                            controllerType.Name),
                                                            "controllerType");
    try
    {
        controller = container.Resolve(controllerType) as IController;
    }
    catch (Exception ex)
    {
        throw new InvalidOperationException(String.Format(
                                "Error resolving controller {0}",
                                controllerType.Name), ex);
    }
    return controller;
}

}

Any suggestions?

Thanks in advance!

like image 900
Kassem Avatar asked Feb 11 '11 22:02

Kassem


1 Answers

This has nothing to do with your controller factory specifically, but it is something you can easily address.

If you are using a Webkit browser (Chrome specifically, Safari too- I think), a request to any website will automatically be accompanied by a request to '/favicon.ico'. The browser is attempting to find a shortcut icon to accompany your website and (for whatever reason) the default path for shortcut icons has been standardized to be '/favicon.ico'.

To avoid the error you're getting, simply define an IgnoreRoute() within the routing table of your MVC web application:

RouteTable.Routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" });

This will ensure that any request to '/favicon.ico' (or '/favicon.gif') will not be handled by MVC.

like image 159
Nathan Taylor Avatar answered Nov 16 '22 02:11

Nathan Taylor