Response.OutputStream didn't work in my controller after updating from Orchard 1.6 to 1.7

Topics: Core
Sep 4, 2013 at 12:44 PM

We are experiencing a weird problem with a module after upgrading to 1.7.
We developed a module for returning a css based on the site configuration.
The point is the controller writes the output in Response.OutputStream.

It worked properly in Orchard 1.6 however we find that it doesn't work on Orchard 1.7. Now what is written in Response.OutputStream seems to be ignored and it doesn't returns anything.

We've debuged it and we have checked that something is being written in Response.OutputStream.

Any clue of what is happening and how to solve it?

Sep 4, 2013 at 5:46 PM
I have to admit that I don't have a clue, but I am curious about why you would use Response.OutputStream from a controller? This sounds very wrong.
Sep 5, 2013 at 7:39 AM
We know it is not very elegant, but it was a decision based on performance. We generate the css reading a css file stored within the theme files. We use a kind of variables within the css file which we replace with values based on a configuration for the tenant. The point is that for improving memory usage we don't load the complete css file, instead we read each line, replace the variables we find and then write to the outputStream.

However despite we have tried that our controler returns a FileResult it stills doesn't work. It should be the expected way in a MVC App, isn't it?

Looking for a solution we have found this thread:

The problem described is similar to the one we experience, with the difference that he returns a ContentResult.
In fact after disabling the OutputCahce module the issue is fixed.

After taking a look to the OutputCache module we have found problem is in the OutputCacheFilter.cs file:
      // only for ViewResult right now, as we don't want to handle redirects, HttpNotFound, ...
         if (filterContext.Result as ViewResultBase == null) {
             Logger.Debug("Ignoring none ViewResult response");
So how we are not returning a View, it ends the method and what we write is ignored.

A solution can be to add a view that returns the css instead of using previous solution, however it looks a bit annoying the different bechavior when you enable/disable OutputCache.
Sep 5, 2013 at 7:50 AM
I use dot less for my dynamic CSS, I read the files in and use a regular expression to change the value of the variables.

There is an attribute that can be added to the action to disable the OutputCache, that is how I fixed the issue without disabling the entire OutputCache module.
Sep 5, 2013 at 10:46 AM
Edited Sep 5, 2013 at 11:54 AM
I see, do you mean sth like this?

[OutputCache(NoStore = true)]
public ActionResult NonCacheableData()

But the desirable solution will be thee css were cached. Without the need of doing that work in the view, isn't it?

BertrandLeRoy do you think I should report it as a bug?
Sep 6, 2013 at 12:19 AM
Yes, you should be using a file result. And yes, you should file a bug, once you have verified that this is not a duplicate. I've seen a number of similar issues with outputcache recently. Thanks.
Sep 6, 2013 at 6:33 AM
I have just spent 2 days tearing my hair out over why my simple Custom Orchard Module, with a FileResult return type from a controller was returning an empty response under IIS, but working fine from within Visual Studio.

public FileResult GetDoc(string docRef)
//Get Doc from DB
return new FileContentResult(doc.DocumentBody, doc.GetMimeType());

For me, if I used Visual Studio/IIS Express, the file content returned OK.
When I deployed to IIS, the content-length returned was 0.
Checked all the headers etc with Fiddler, no difference, except for the Content-Length being 0.
I was about to give up on Orchard, until I found this post!
I set a breakpoint in OutputCacheFilter.cs as described by @jersiovic above, and that was it!

Disabled the Output Cache module, and file downloads are now working fine!
Sep 6, 2013 at 8:33 AM
Happy to hear that MuzFrancis.

I have filed a bug
Camon vote for it! ;)
Oct 7, 2013 at 12:44 PM
I just voted for this bug. It took me a long time to find out what this was. It first seemed related to authorization because is won't happen when you're logged in (off course the cache is not enabled when logged in)
Oct 7, 2013 at 9:54 PM
On a related note, if you care about performance, why don't you save the generated CSS as a static file to media? You could then re-generate it every time the config changes.
Oct 8, 2013 at 4:46 AM
What you propose, is best solution, thank you for pointing it ;)
The point is that by the moment configuration is saved in a text file by a technician instead of using Orchard admin pages. To pass it to Orchard Admin panel by the moment is not a priority todo point in our project. So we don't have a trigger for regenerating css when config has changed or when our CSS generator module has changed. So most easy solution for us is to rely on the cache. module for improving performance.