You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

旧版ASP.NET(C#)站点如何处理/data:image/x-icon;的GET请求?

处理旧版ASP.NET站点中/data:image/x-icon;请求的问题

我判断这类请求是针对Favicon的,但半年前从未出现过。目前已有多个客户端定期发起该请求,导致请求跳转到ErrorPage.aspx,不确定在旧版ASP.NET(C#)站点中该如何正确处理。

请求日志如下:

2024-12-17 GET /data:image/x-icon;, - 443 - 99.99.99.99 Mozilla/5.0+(X11;+Linux+x86_64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/124.0.0.0+Safari/537.36 - 302 0 0 45
2024-12-17 GET /ErrorPage.aspx aspxerrorpath=/data:image/x-icon;, 443 - 99.99.99.99 Mozilla/5.0+(X11;+Linux+x86_64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/124.0.0.0+Safari/537.36 - 200 0 0 72

补充信息:同一客户端发起该请求前4秒使用的User-Agent为:

Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/131.0.0.0+Safari/537.36

服务器运行在Windows Server 2016系统上,以下是具体处理方案:

ASP.NET代码层面处理

  • 在Global.asax的Application_BeginRequest事件中添加拦截逻辑:
    若不需要返回图标,直接返回204状态码避免跳转到错误页:
protected void Application_BeginRequest(object sender, EventArgs e)
{
    string path = Request.Path;
    if (path.Equals("/data:image/x-icon;", StringComparison.OrdinalIgnoreCase))
    {
        Response.StatusCode = 204;
        Response.End();
    }
}

若站点有实际的favicon.ico文件,可重定向到正确路径:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string path = Request.Path;
    if (path.Equals("/data:image/x-icon;", StringComparison.OrdinalIgnoreCase))
    {
        Response.Redirect("/favicon.ico", false);
        Context.ApplicationInstance.CompleteRequest();
    }
}

IIS配置层面处理

  1. 打开IIS管理器,定位到目标站点,进入URL重写模块
  2. 添加空白规则,名称设为"处理无效Favicon请求"
    • 若要直接拦截返回204:
      • 匹配URL模式设为^data:image/x-icon;$,使用正则表达式
      • 操作选择"自定义响应",状态码填204,状态描述和响应内容留空
    • 若要重定向到正确图标:
      • 匹配URL模式同上
      • 操作选择"重定向",重定向URL填/favicon.ico,状态码选301永久重定向

内容的提问来源于stack exchange,提问作者Robert Koernke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 12:57:48