1. 程式人生 > >mvc生成靜態頁 獲取controller的HTML

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");