请问如何获取MVC 视图执行后的内容?
我想生成个HTML文件,如何获取如何获取MVC 视图执行后的内容?
public override void OnResultExecuted(ResultExecutedContext filterContext)
filterContext 怎么输出 视图执行过后的内容呢
------解决方案--------------------我用的方法:
public static string RenderRazorViewToString(this ControllerBase controller, string viewName, string master,object model)
{
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = (string.IsNullOrWhiteSpace(master) ? ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName) : ViewEngines.Engines.FindView(controller.ControllerContext, viewName, master));
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
使用方法:
public ActionResult MyAction(){
return Content(this.RenderRazorViewToString("About","_Layout", myModel);
}