我有一個簡單的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