码迷,mamicode.com
首页 > Web开发 > 详细

mvc生成静态页

时间:2017-11-08 14:53:14      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:目录   text   view   htm   obj   iter   ret   using   html   

 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生成静态页

标签:目录   text   view   htm   obj   iter   ret   using   html   

原文地址:http://www.cnblogs.com/windseasons/p/7803582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!