mvc生成静态页 获取controller的HTML

  public  string GetViewHtml(Controller controller, string viewName, string masterName)
        {
            IView view = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, masterName).View;
            if (view == null) return "";
            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();
            }
        }
// 参数1  控制器对象   参数2 视图名  参数3模板页名称
                    string htmlcode = GetViewHtml(this, "NewsInfo", "_Layout");

你可能感兴趣的:(C#,ASP.NET,MVC)