2011-02-11 50 views
10

我最近加入微软统一到我的MVC3项目,现在我得到这个错误:问题与自定义控制器工厂

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

我真的没有一个favicon.ico的,所以我不知道在哪里这是来自。而最奇怪的是,该视图实际上正在呈现,然后这个错误正在被抛出......我不知道,如果它是我的控制器工厂类错误,因为我从一些教程(我不是IoC - 这是我第一次这样做)。代码如下:

公共类UnityControllerFactory:DefaultControllerFactory { IUnityContainer容器;

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; 
} 

}

有什么建议?

提前致谢!

回答

29

这与您的控制器工厂没有任何关系,但它是您可以轻松解决的问题。

如果您使用的是Webkit浏览器(Chrome特别是Safari,我认为),对任何网站的请求都会自动附带对“/favicon.ico”的请求。浏览器正试图找到一个快捷方式图标来伴随您的网站和(无论什么原因)默认快捷方式图标的路径已标准化为'/favicon.ico'。

为避免你得到的错误,只是你的MVC Web应用程序的路由表中定义一个IgnoreRoute():

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

这将确保任何请求“/favicon.ico”(或'/favicon.gif')将不会由MVC处理。

+0

非常感谢弥敦道!这实际上是:) – Kassem 2011-02-11 22:39:13

0

我已经看到这种情况还有:

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