我有一个非常基本的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));
}
对于我网站上的某些页面(我找不到押韵或原因),我得到了“HTTP Web例外:无效使用响应过滤器”,可能有90%的时间。只需刷新十几次就能获得正确的输出,但再次刷新会显示异常:
[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 :(得分:0)
我可能应该更清楚地知道:不应该共享响应过滤器。我每个类都使用一个对象,每次调用OnActionExecuted
函数时都会创建一个新的响应过滤器来解决问题。