Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the generated html file inside an action

I have a MVC Controller which provides a simple View

public class MyController : Controller {
    [HttpGet]
    public ActionResult Index() {
        return View();
    }

    [HttpGet]
    public ActionResult ZipIndex() {
        // Get the file returned bu Index() and zip it 
        return File(/* zip stream */);
    }
}

As you can see from above what I need to implement is a method that gets the html generated by Index(), zip it and return it as a file that can be downloaded.

I know how to zip, but I don't know how to get the html.

like image 856
Ilir Deda Avatar asked Mar 28 '14 16:03

Ilir Deda


1 Answers

Check this post out http://approache.com/blog/render-any-aspnet-mvc-actionresult-to/. It gives a neat way to render the output of any ActionResult to a string.

Edit

Building on the technique outlined in the above article, a complete solution could look like this

using System.IO;
using System.IO.Compression;
using System.Web;

public class MyController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public FileContentResult ZipIndex()
    {
        // Render the View output: 
        var viewString = View("TheViewToRender").Capture(ControllerContext);
        // Create a zip file containing the resulting markup
        using (MemoryStream outputStream = new MemoryStream())
        {
            StreamReader sr = new StringReader(viewString);
            using (ZipArchive zip = new ZipArchive(outputStream, ZipArchiveMode.Create, false))
            {
                ZipArchiveEntry entry = zip.CreateEntry("MyView.html", CompressionLevel.Optimal);
                using (var entryStream = entry.Open())
                {
                    sr.BaseStream.CopyTo(entryStream);
                }
            }
            return File(outputStream.ToArray(), MediaTypeNames.Application.Zip, "Filename.zip");
        }
    }
}

public static class ActionResultExtensions {
    public static string Capture(this ActionResult result, ControllerContext controllerContext) {
        using (var it = new ResponseCapture(controllerContext.RequestContext.HttpContext.Response)) {
            result.ExecuteResult(controllerContext);
            return it.ToString();
        }
    }
}
public class ResponseCapture : IDisposable {
    private readonly HttpResponseBase response;
    private readonly TextWriter originalWriter;
    private StringWriter localWriter;
    public ResponseCapture(HttpResponseBase response) {
        this.response = response;
        originalWriter = response.Output;
        localWriter = new StringWriter();
        response.Output = localWriter;
    }
    public override string ToString() {
        localWriter.Flush();
        return localWriter.ToString();
    }
    public void Dispose() {
        if (localWriter != null) {
            localWriter.Dispose();
            localWriter = null;
            response.Output = originalWriter;
        }
    }
}
like image 180
Paul Taylor Avatar answered Oct 02 '22 05:10

Paul Taylor