1,输出View HTML 字符串:
代码如下 | 复制代码 |
/// /// 描述:输出View HTML 字符串 /// /// /// 视图文件名 /// 母板页文件名 /// protected static string RenderViewToString(Controller controller, string viewName, string masterName) { IView view = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, masterName).View; using (StringWriter writer = new StringWriter()) { ViewContext viewContext = new ViewContext(controller.ControllerContext, view, controller.ViewData, controller.TempData, writer); viewContext.View.Render(viewContext, writer); return writer.ToString(); } } |
2,输出PartialView HTML 字符串:
代码如下 | 复制代码 |
/// /// 描述:输出PartialView HTML 字符串 /// /// /// 部分视图文件名 /// protected static string RenderPartialViewToString(Controller controller, string partialViewName) { IView view = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialViewName).View; using (StringWriter writer = new StringWriter()) { ViewContext viewContext = new ViewContext(controller.ControllerContext, view, controller.ViewData, controller.TempData, writer); viewContext.View.Render(viewContext, writer); return writer.ToString(); } } |