Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC2 (RTM) breaks response filtering - "Filtering is not allowed"

I've just done a test run of upgrading a project to ASP.Net MVC 2 (RTM) in anticipation of the full official .Net 4.0 release coming later this month.

Our application is using a minimizer for our CSS and javascript. To do so, it is making use of the HttpResponse.Filter property to set a custom filter.

With the upgrade, the setter for this property is throwing an HttpException saying "Filtering is not allowed." Looking that the HttpResponse.Filter property in reflector shows this:

  set
    {
        if (!this.UsingHttpWriter)
        {
            throw new HttpException(SR.GetString("Filtering_not_allowed"));
        }

...

private bool UsingHttpWriter
{
    get
    {
        return ((this._httpWriter != null) && (this._writer == this._httpWriter));
    }
}

Clearly something has changed in the way the HttpResponse is writing to the output stream in MVC2. Does anyone know what the change is, or at least a workaround for this?

EDIT: This seems pretty radical. Some further investigation shows that ASP.Net MVC 2 RTM is using a System.Web.Mvc.ViewPage.SwitchWriter as the Output property of an HttpResponse, whereas MVC 1 was using a plain old HttpWriter. That explains why the exception is being thrown.

But that doesn't explain why they've chosen to completely break this functionality. This thread seems to indicate that this is just temporary... but this makes me pretty nervous... this is the RTM after all.

Any further comments appreciated on this.

like image 332
womp Avatar asked Apr 06 '10 18:04

womp


1 Answers

Views in MVC, unlike regular .aspx pages, are meant to be treated as HTML templating systems. The problem here stems from the fact that that after the view had already started executing, the page tried to install a response filter in order to manipulate the HTML being sent to the client.

The solution in this case is to move the response filter installation to an earlier part of the MVC pipeline. For example, using a custom action filter, you can override the OnResultExecuting() method to install your custom response filter. This moves the installation out of the view and doesn't violate the principle of simple HTML-only views.

like image 95
Levi Avatar answered Nov 13 '22 06:11

Levi