WCF (asp.net アプリケーションではない) で動作する単純な html および javascript クライアント アプリケーションがあります。静的ページでいくつかの変数を変更する必要があるため、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 8 で IIS 7.5 を使用しています。
contentInBuffer 変数の値として Write メソッドで ContentFilter.cs をデバッグしているときにこれらを見ました。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�|���_�n� ...