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

前端 未结 1 1939
一个人的身影
一个人的身影 2021-01-02 08: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-crea

相关标签:
1条回答
  • 2021-01-02 09:25

    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();
      }
    }
    
    0 讨论(0)
提交回复
热议问题