2012-11-14 78 views
2

我有一個非常基本的ASP.NET響應過濾器,工作「很好」。我用它來替換靜態資源的域。重寫ASP.NET輸出時出現「無效使用響應過濾器」

我在控制器代碼如下所示:

protected override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
      _cdnRewriter.Stream = filterContext.RequestContext.HttpContext.Response.Filter; 
      filterContext.RequestContext.HttpContext.Response.Filter = _cdnRewriter; 
    } 

而且重寫過濾器本身:

public override void Write(byte[] buffer, int offset, int count) 
    { 
     var html = Encoding.Default.GetString(buffer, offset, count); 

     //manipulate html 

     byte[] outData = Encoding.Default.GetBytes(html); 
     Stream.Write(outData, 0, outData.GetLength(0)); 
    } 

有關我的網站某些網頁(我找不到情由然而),我可能會有90%的時間出現「HTTP Web Exception:無效使用響應過濾器」。只需刷新了十幾次會得到正確的輸出,但再次刷新將顯示異常:

[HttpException (0x80004005): Invalid use of response filter] 
    System.Web.HttpResponseStreamFilterSink.VerifyState() +4097234 
    System.Web.HttpResponseStreamFilterSink.Write(Byte[] buffer, Int32 offset, Int32 count) +28 
    NeoSmart.Web.CdnRewriteFilter.Write(Byte[] buffer, Int32 offset, Int32 count) +452 
    System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +359 
    System.Web.HttpResponse.FilterOutput() +121 
    System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +119 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +165 

我做得不對,我響應濾波器?

回答

0

我可能應該知道更好:不應共享響應過濾器。我爲每個類使用一個對象,每次調用OnActionExecuted函數時都會創建一個新的響應篩選器,以解決問題。

+0

如果您可以發佈代碼,這將非常有幫助。 – qakmak