10

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!

2 Answers 2

29

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot Nathan! That actually did it :)
I used this code in the Global.asax and now I get a "File does not exist" exception in Chrome.
0

I have seen this done as well:

    catch (Exception ex)
{
    /*throw new InvalidOperationException(String.Format(
                            "Error resolving controller {0}",
                            controllerType.Name), ex);*/
    base.GetControllerInstance(requestContext,controllerType);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.