asp.net-mvcms-wordviewresult

How to get the content of a ViewResult in MVC?


I have a view that is rendered with ContentType of "application/msword". For users this appears as download file. Now, I would like to save this document server side. How could get I access the rendered document server side ?


Solution

  • using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream))
    {
        var viewContext =
            new ViewContext(
                filterContext.Controller.ControllerContext,
                vResult.View,
                vResult.ViewData,
                vResult.TempData,
                writer);
    
        vResult.View.Render(viewContext, writer);
        writer.Flush();
    }