zoukankan      html  css  js  c++  java
  • mvc生成静态页

     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;
    
            }

  • 相关阅读:
    Python 函数与函数式编程
    Python 字符编码与转码
    Python 读写txt文件操作
    两阶段事务总结
    MPPDB集群高可用设计
    MPPDB中的各个组件
    IntelliJ IDEA2016学习小结
    mysql免安装版配置
    理想的智能机
    java对象的大小
  • 原文地址:https://www.cnblogs.com/windseasons/p/7803582.html
Copyright © 2011-2022 走看看