sách gpt4 ai đã đi

asp.net-mvc - 自定义 Controller 工厂的问题

In lại 作者:行者123 更新时间:2023-12-02 04:36:34 26 4
mua khóa gpt4 Nike

我最近将 Microsoft Unity 添加到我的 MVC3 项目中,现在收到此错误:

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

我并没有真正的 favicon.ico,所以我不知道它来自哪里。最奇怪的是, View 实际上正在渲染,然后抛出这个错误...我不确定我的 Controller 工厂类是否有问题,因为我从一些教程中获取了代码(我不是 IoC - 这是我第一次这样做)。代码如下:

公共(public)类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");
thử
{
controller = container.Resolve(controllerType) as IController;
}
catch (Exception ex)
{
throw new InvalidOperationException(String.Format(
"Error resolving controller {0}",
controllerType.Name), ex);
}
return controller;
}

}

Có gợi ý nào không?

Cảm ơn trước nhé!

1 Câu trả lời

这与您的 Controller 工厂无关,但您可以轻松解决。

如果您使用的是 Webkit 浏览器(特别是 Chrome,我认为还有 Safari),对任何网站的请求都会自动伴随对“/favicon.ico”的请求。浏览器正在尝试查找与您的网站配套的快捷方式图标,并且(无论出于何种原因)快捷方式图标的默认路径已标准化为“/favicon.ico”。

为了避免出现错误,只需在 MVC Web 应用程序的路由表中定义 IgnoreRoute() 即可:

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

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

关于asp.net-mvc - 自定义 Controller 工厂的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4974700/

26 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com