gpt4 book ai didi

javascript - HttpModule Filter_OnTransformString 导致部分静态文件失效(即css、js

转载 作者:太空宇宙 更新时间:2023-11-03 14:42:17 25 4
gpt4 key购买 nike

我有一个 HttpModule 来分析和监控网站。在这个 HttpModule 中,它使用响应过滤器来修改响应主体并插入一段代码以将 javascript 代码加载到文本/html 内容中。

private string Filter_OnTransformString(string output)
{
Logger.Info("Filter_OnTransformString", "Enter method for " + Request.RawUrl);

if (Response.ContentType != "text/html")
{
Logger.Info("Filter_OnTransformString", "Exiting for " + Response.ContentType + " MIME type for " + Request.RawUrl);
return output;
}

if (output != null)
{
var pos = output.IndexOf("</body>", StringComparison.InvariantCultureIgnoreCase);
Logger.Verbose("Filter_OnTransformString", "pos (of closing </body>) == " + pos);

if (pos > -1)
{
var left = output.Substring(0, pos);
var right = output.Substring(pos);

output = left + "<script type=\"text/javascript\" src=\"/heartbeat.axd/script\"></script>" + right;
}
}
else
{
Logger.Warning("Filter_OnTransformString", "output == null for " + Request.RawUrl);
}

Logger.Info("Filter_OnTransformString", "Exit method for " + Request.RawUrl);

return output;
}

查看 Chrome DevTools 中的网络流量,我看到 bootstrap.css、jquery-ui.min.css、jquery-3.1.1.js 和 respond.js 都失败了。

enter image description here

然而,其他 CSS 和 JS 文件都正常运行。例如,这是 modernizr-2.8.3.js 的日志文件输出,后面是 jquery-3.1.1.js 的日志文件输出。

11:19:31.1695   Info    Filter_OnTransformString    Enter method for /Scripts/modernizr-2.8.3.js
11:19:31.1695 Info Filter_OnTransformString Exiting for application/javascript MIME type for /Scripts/modernizr-2.8.3.js


11:19:31.5914 Info Filter_OnTransformString Enter method for /Scripts/jquery-3.1.1.js
11:19:31.5914 Info Filter_OnTransformString Exiting for application/javascript MIME type for /Scripts/jquery-3.1.1.js

我在这里遗漏了什么重要的东西吗?

更新:

目前,我围绕这个问题编写了如下代码:

if (!Request.RawUrl.EndsWithAny(new[] { ".css", ".js" }, StringComparison.InvariantCultureIgnoreCase))
{
Logger.Verbose("Application_BeginRequest", "Attaching Response.Filter to " + Request.RawUrl);
ResponseFilterStream filter = new ResponseFilterStream(Response.Filter);
filter.TransformString += Filter_OnTransformString;
Response.Filter = filter;
}

最佳答案

你的语法对于 set output var 是不正确的:

output = left + "<script type="text/javascript" src="/heartbeat.axd/script"></script>" + right;

连接的正确形式是:

output = left + "<script type='text/javascript' src='/heartbeat.axd/script'></script>" + right;

可能这会产生异常并且不会加载您的静态文件。

关于javascript - HttpModule Filter_OnTransformString 导致部分静态文件失效(即css、js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56261064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com