December 12, 2008

WORKAROUND: ASP.NET "Response filter is not valid."

Problem:

ASP.NET 3.5 web site. You are trying to set the Response.Filter property and you are getting an HttpException with the message "Response filter is not valid."

Cause:

There is a bug in ASP.NET 3.5 where you cannot set the Response.Filter property unless you read it first. When the Response.Filter property is first read, it instantiates some internal fields in the HttpWriter class.

Workaround:

Just read the property first so that it initializes the internal properties accordingly.

In VB:
Dim f = Response.Filter ' Assumes Option Infer On
Response.Filter = New FilterStream(Response.OutputStream) ' Where FilterStream is your stream
In C#:
var f = Response.Filter;
Response.Filter = new FilterStream(Response.OutputStream);

3 comments:

Tom said...

This saved me much tearing of hair. Many thanks!

easyfan2006 said...

Worked for me in ASP.NET 2.0! Thanks!

Red Cell Innovation Inc. said...

Thank you! I thought I was going crazy. Yvan Rodrigues