31

For some strange reasons, I want to write HTML directly to the Response stream from a controller action. (I understand MVC separation, but this is a special case.)

Can I write directly into the HttpResponse stream? In that case, which IView object should the controller action should return? Can I return 'null'?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Palani
  • 8,962
  • 11
  • 53
  • 62

5 Answers5

50

I used a class derived from FileResult to achieve this using normal MVC pattern:

/// <summary>
/// MVC action result that generates the file content using a delegate that writes the content directly to the output stream.
/// </summary>
public class FileGeneratingResult : FileResult
{
    /// <summary>
    /// The delegate that will generate the file content.
    /// </summary>
    private readonly Action<System.IO.Stream> content;

    private readonly bool bufferOutput;

    /// <summary>
    /// Initializes a new instance of the <see cref="FileGeneratingResult" /> class.
    /// </summary>
    /// <param name="fileName">Name of the file.</param>
    /// <param name="contentType">Type of the content.</param>
    /// <param name="content">Delegate with Stream parameter. This is the stream to which content should be written.</param>
    /// <param name="bufferOutput">use output buffering. Set to false for large files to prevent OutOfMemoryException.</param>
    public FileGeneratingResult(string fileName, string contentType, Action<System.IO.Stream> content,bool bufferOutput=true)
        : base(contentType)
    {
        if (content == null)
            throw new ArgumentNullException("content");

        this.content = content;
        this.bufferOutput = bufferOutput;
        FileDownloadName = fileName;
    }

    /// <summary>
    /// Writes the file to the response.
    /// </summary>
    /// <param name="response">The response object.</param>
    protected override void WriteFile(System.Web.HttpResponseBase response)
    {
        response.Buffer = bufferOutput;
        content(response.OutputStream);
    }
}

The controller method would now be like this:

public ActionResult Export(int id)
{
    return new FileGeneratingResult(id + ".csv", "text/csv",
        stream => this.GenerateExportFile(id, stream));
}

public void GenerateExportFile(int id, Stream stream)
{
    stream.Write(/**/);
}

Note that if buffering is turned off,

stream.Write(/**/);

becomes extremely slow. The solution is to use a BufferedStream. Doing so improved performance by approximately 100x in one case. See

Unbuffered Output Very Slow

Community
  • 1
  • 1
Knaģis
  • 20,827
  • 7
  • 66
  • 80
  • Best answer - just add the file once, and reuse this concept in every other situation using the flexible delegate parameter. – Froyke Jan 24 '13 at 18:35
  • Note if you write a large file in this manner, you may get an `OutOfMemoryException`. You can resolve by turning off buffering. Add a line to `WriteFile()` like this: `response.Buffer = false;` – Eric J. Sep 24 '14 at 06:49
  • Nice solution. +1 Took liberty of rolling @EricJ. 's suggestion into this answer (and updating xmldocs). Feel free to roll back if this offends. – spender Sep 25 '14 at 02:06
  • @spender: I discovered a caveat... turning off buffering makes the output *extremely* slow. Adding in a BufferedStream resolves that. I updated the answer with that information. – Eric J. Sep 25 '14 at 16:00
  • 1
    Does this work if the writing is done asynchronously and the WriteFile method returns before the request is complete? I can't find anything by googling for "FileResult" and "async". – John Oct 28 '14 at 13:21
  • is this solution able to use in aspnet core? – liang Aug 06 '17 at 17:29
  • @liang see https://stackoverflow.com/questions/33178983/how-to-create-a-response-message-and-add-content-string-to-it-in-asp-net-5-mvc – Professor of programming Jan 03 '18 at 11:06
  • Im trying to use your solution with Xml but the result is an empty xml file return new FileGeneratingResult(fileName, "text/xml", stream => xmlDocument.WriteTo(new XmlTextWriter(stream, Encoding.UTF8))); am I missing something? – Michael Brennt Mar 20 '19 at 17:03
9

Yes, you can write directly to the Response. After you're done, you can call CompleteRequest() and you shouldn't need to return anything.

For example:

// GET: /Test/Edit/5
public ActionResult Edit(int id)
{

    Response.Write("hi");
    HttpContext.ApplicationInstance.CompleteRequest();

    return View();     // does not execute!
}
womp
  • 115,835
  • 26
  • 236
  • 269
6

If you don't want to derive your own result type, you can simply write to Response.OutputStream and return new EmptyResult().

G-Wiz
  • 7,370
  • 1
  • 36
  • 47
5

Write your own Action Result. Here's an example of one of mine:

public class RssResult : ActionResult
{
    public RssFeed RssFeed { get; set; }

    public RssResult(RssFeed feed) {
        RssFeed = feed;
    }

    public override void ExecuteResult(ControllerContext context) {
        context.HttpContext.Response.ContentType = "application/rss+xml";
        SyndicationResourceSaveSettings settings = new SyndicationResourceSaveSettings();
        settings.CharacterEncoding = new UTF8Encoding(false);
        RssFeed.Save(context.HttpContext.Response.OutputStream, settings);
    }
}
John Sheehan
  • 77,456
  • 30
  • 160
  • 194
3

You can do return Content(...); where, if I remember correctly, ... would be what you want to write directly to the output stream, or nothing at all.

Take a look at the Content methods on the Controller: http://aspnet.codeplex.com/SourceControl/changeset/view/22907#266451

And the ContentResult: http://aspnet.codeplex.com/SourceControl/changeset/view/22907#266450

Jordan S. Jones
  • 13,703
  • 5
  • 44
  • 49