Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to save an MVC Razor view into an actual html file

We are building an MVC app that creates physical HTML pages. The app currently creates the pages dynamically using the normal MVC/Razor approach.

Rather than re-creating the output programatically to a file, is there anyway to grab the result built by razor and save it to a file?

Thanks so much!

like image 913
Julian Dormon Avatar asked Mar 15 '14 00:03

Julian Dormon


1 Answers

you can render the view to string, then save the string in a file ...

public string RenderViewToString(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();
  }
}
like image 150
Benoit Avatar answered Nov 18 '22 04:11

Benoit