【发布时间】:2012-10-31 13:25:01
【问题描述】:
我有一个使用 WCF 的简单 html 和 javascript 客户端应用程序(不是 asp.net 应用程序)。我需要更改静态页面中的一些变量,所以我认为 Response.Filter 是最适合我的选项。我写了几行代码,它工作了,但是在我在浏览器上刷新了几次之后,我注意到有一个错误。某些东西破坏了页面的编码。我做错了什么?
Global.asax:(我也尝试了其他事件,但没有任何变化)
private void Application_PostReleaseRequestState(object sender, System.EventArgs e)
{
if (Request.CurrentExecutionFilePathExtension.EndsWith(".html") || Request.CurrentExecutionFilePathExtension.EndsWith(".js"))
{
Response.Filter = new ContentFilter(Response.Filter);
}
}
内容过滤器.cs
public class ContentFilter : MemoryStream
{
private Stream outputStream = null;
private Regex version = new Regex("%version%", RegexOptions.Compiled | RegexOptions.Multiline);
public ContentFilter(Stream output)
{
outputStream = output;
}
public override void Write(byte[] buffer, int offset, int count)
{
// Convert the content in buffer to a string
string contentInBuffer = UTF8Encoding.UTF8.GetString(buffer);
contentInBuffer = version.Replace(contentInBuffer, "2");
outputStream.Write(UTF8Encoding.UTF8.GetBytes(contentInBuffer), offset, UTF8Encoding.UTF8.GetByteCount(contentInBuffer));
}
}
注意:我在 Windows 8 上使用 IIS 7.5。
当我在 Write 方法中调试 ContentFilter.cs 作为 contentInBuffer 变量的值时,我看到了这些。我在 IIS 设置中默认有 GZIP 压缩,也许就是这样。
`�\b\0\0\0\0\0\0�Z�n�����w3\b(�\"�VD�I���8A�a���r �����,m�\t��>@�����t�\n(P�/��+��]���$���B�3s�|���_不...
【问题讨论】:
-
您在 Fiddler 中检查过您的响应内容吗?我见过几个与 Response.Filter 相关的问题,但它们大多与错误地更新内容有关。我首先验证标题和内容字节。您在回复中使用国际字符吗?
-
是的,有土耳其字符,如 ö、ç、ü、ş 和 ğ。我会用提琴手检查它。我不明白为什么,但第一次打开页面,刷新 3 次后它开始失败。
-
� 这是我的提琴手的输出。 :)
标签: c# .net iis filtering httpresponse