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

蓝咒 提交于 2019-11-29 19:09:43

问题


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!


回答1:


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();
  }
}


来源:https://stackoverflow.com/questions/22418176/is-it-possible-to-save-an-mvc-razor-view-into-an-actual-html-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!