Is it possible to save an MVC Razor view into an a

2019-02-12 19:40发布

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!

2条回答
劳资没心,怎么记你
2楼-- · 2019-02-12 19:42

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();
  }
}
查看更多
做自己的国王
3楼-- · 2019-02-12 20:00

You can just save the web page when you view it in a browser. You could script this programmatically with any language.

However, if you're using any .net controls running server side this will cause issues if you plan on using ID attributes to code against.

查看更多
登录 后发表回答