是否有可能挽救一个MVC的Razor视图为实际的html文件 [英] Is it possible to save an MVC Razor view into an actual html file
本文介绍了是否有可能挽救一个MVC的Razor视图为实际的html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们正在构建物理创建的HTML页面的MVC应用程序。
该应用程序目前创建动态网页使用普通的MVC /剃刀的办法。
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?
太感谢了!
推荐答案
您可以渲染视图以字符串,然后保存在字符串中的文件...
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();
}
}
这篇关于是否有可能挽救一个MVC的Razor视图为实际的html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文