mvc生成靜態頁
阿新 • • 發佈:2017-11-08
創建 span var eal string create lte test return
private bool GenerateStaticPage(string viewPath, string htmlPath, ControllerContext context, object model = null, bool isPartial = false, string masterName = "") { //創建存放靜態頁面目錄 if (!Directory.Exists(Path.GetDirectoryName(htmlPath))) { Directory.CreateDirectory(Path.GetDirectoryName(htmlPath)); } //刪除已有的靜態頁面 //if (System.IO. File.Exists(htmlPath))//{ // System.IO.File.Delete(htmlPath); //} ViewEngineResult result = null; if (isPartial) { result = ViewEngines.Engines.FindPartialView(context, viewPath); } else { result = ViewEngines.Engines.FindView(context, viewPath, masterName); }if (model != null) { context.Controller.ViewData.Model = model; } if (result.View != null) { using (var sw = new StringWriter()) { var viewContext = new ViewContext(context, result.View, context.Controller.ViewData, context.Controller.TempData, sw); result.View.Render(viewContext, sw); string body = sw.ToString(); System.IO.File.WriteAllText(htmlPath, body, System.Text.Encoding.UTF8); } } else { } return true; }
mvc生成靜態頁