MVC and CMS.OutputFilter

Daniel Reed asked on February 22, 2016 03:35

I have a Kentico installation with a custom MVC controller that downloads a file. I have attached the code below.

I have verified (by putting this code into a different MVC project) that it works fine. However, in Kentico, the document is unreadable and even file sizes are different.

After a couple of hours I managed to determine that commenting out the OutputFilter in the web.config resolved my problem.

<add name="XHtmlModule" type="CMS.OutputFilter.OutputFilterModule, CMS.OutputFilter" />

The question I have is - is there a way to deactivate the output filter for an MVC controller in some way? Obviously removing it entirely is not the appropriate solution.

    [HttpGet, Authorize]
    public FileContentResult Download(string id)
    {
        var path = @"C:\Windows\TEMP\file.pdf";
        if (path != null && System.IO.File.Exists(path))
        {
            string name = Path.GetFileName(path);
            byte[] data = System.IO.File.ReadAllBytes(path);
            string contentType = MimeMapping.GetMimeMapping(path);

            var cd = new ContentDisposition
            {
                FileName = name,
                Inline = true,
            };

            Response.AppendHeader("Content-Disposition", cd.ToString());

            return File(data, contentType);
        }
        throw new HttpException(404, "File not found");
    }

Correct Answer

Daniel Reed answered on February 23, 2016 03:32

I did try that actually, but no luck.

I used JustDecompile to see how the OutputFilter worked, and then added the following code to fix the problem:

OutputFilterContext.ApplyOutputFilter = false;
ResponseOutputFilter.UseOutputFilterCache = false;
0 votesVote for this answer Unmark Correct answer

Recent Answers


Roman Hutnyk answered on February 22, 2016 14:26

Have you tried to exclude the URL of your download method from output filter? - This could be done in Settings -> System -> Output filter.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.