HttpContext.Current.Response.Filter:响应过滤器无效

Dav*_*amp 5 c# asp.net

繁忙的一些遗留代码写入csv/xls:

// Set up response
string filename = "ErrorControlExport";
string attachment = "attachment; filename=" + filename + "_" + DateTime.UtcNow.ToFileTime() + "." + extension;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Pragma", "public");
// Clear any settings (such as GZip)
HttpContext.Current.Response.Filter = null;//exception occurs here

// Add BOM to force recognition of UTF-8
byte[] bom = new byte[] { 0xef, 0xbb, 0xbf };
HttpContext.Current.Response.BinaryWrite(bom);
Run Code Online (Sandbox Code Playgroud)

但是当代码到达时:

// Clear any settings (such as GZip)
HttpContext.Current.Response.Filter = null;//exception occurs here
Run Code Online (Sandbox Code Playgroud)

它抛出一个HttpException

响应过滤器无效.

所以我的问题是:

1)如何将过滤器设置为空或默认过滤器?

2)甚至是必要的吗?

在此先感谢您的帮助

小智 9

查看这篇文章.即使它谈论ASp.Net 3.5,我尝试了它,它的工作原理.我只是阅读过滤器var filter = Response.Filter,然后为其分配nullResponse.Filter = null