我有一个简单的HTML和JavaScript客户端应用程序与WCF(而不是一个asp.net应用程序)一起工作。我需要在静态页面中更改一些变量,因此我认为Response.Filter对我来说是最合适的选择。我写了几行代码,它工作正常,但是在浏览器刷新几次之后,我发现有一个错误。有东西破坏了页面的编码。我究竟做错了什么?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);
}
}
ContentFilter.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上使用IIS 7.5 8.
我看到这些,而我在调试ContentFilter.cs写我作为contentInBuffer变量的值。默认情况下,我在IIS设置中使用GZIP压缩,这可能就是这样。
`\ B \ 0 \ 0 \ 0 \ 0 \ 0 \ 0Znw3 \ B(\“VDI8A5a R,M \ T> @t \ N(P/+] $B3s| _n ...
您是否检查了Fiddler中的响应内容?我已经看到了与Response.Filter相关的几个问题,但它们大多与错误地更新内容有关。我首先要验证标题和内容字节。你在回复中使用国际角色吗? – 2012-11-12 04:44:42
是的,有土耳其的字符,如ö,ç,ü,ş和»。我会用提琴检查它。我不明白为什么,但第一次打开页面,3次刷新后就开始失败。 – iboware
这是我的提琴手的输出。 :) – iboware