Download View instead of displaying it

1

Is there any way to instead of returning a view and displaying it on the screen for the user, I return it but instead of displaying it I save the .html file on the computer? The code is as follows:

return View("~/Views/Item/Item.cshtml", model);

But it returns the view and displays it for the user, I wanted a way to download the html file instead of displaying it, is it possible to do this in some way?

    
asked by anonymous 09.03.2017 / 20:31

1 answer

2
public string RenderRazorViewToString(string viewName, object model)
{
   ViewData.Model = model;
   using (var sw = new StringWriter())
   {

    var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,viewName);
    var viewContext = new ViewContext(ControllerContext, 
                                 viewResult.View,
                                 ViewData, 
                                 TempData, 
                                 sw);

    viewResult.View.Render(viewContext, sw);
    viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);

    return sw.GetStringBuilder().ToString();

  }
}

and in your controller :

return File(encoding.GetBytes(RenderRazorViewToString("Item", model)),"text/html");

You may have to change the name of path (path) to work, make debug if it does not work, and see what path is looking for View passed as a parameter. p>     

10.03.2017 / 11:40