【asp.net core】ASP.NET MVC 利用Razor引擎生成静态页

时间:2021-06-18  来源:ASP.NET MVC  阅读:

最近在研究ASP.NET MVC生成静态页,那么今天也算个学习笔记吧!

实现原理及步骤:

1.通过ViewEngines.Engines.FindView查找到对应的视图,如果是部分视图,则用:ViewEngines.Engines.FindPartialView;

2.设置上下文对象里的Model;

3.调用视图的Render()方法,将渲染结果保存到物理静态文件;

usingSystem; 

usingSystem.IO; 

usingSystem.Text; 

usingSystem.Web.Mvc; 

   

namespaceWhir.Foundation.UI 

  ///

 

  ///   描述:静态页面生成帮助类 

  /// 

  publicclassStaticPageHelper 

  { 

    ///

 

    ///   根据View视图生成静态页面 

    /// 

    ///存放静态页面所在绝对路径 

    ///ControllerContext 

    ///视图名称 

    ///模板视图名称 

    ///参数实体模型 

    ///返回信息 

    ///是否分布视图 

    ///生成成功返回true,失败false 

    publicstaticAjaxResult GenerateStaticPage(stringviewPath, 

                          stringhtmlPath, 

                          ControllerContext context,objectmodel =null,boolisPartial =false, 

                          stringmasterName ="") 

    { 

      var ajaxResult =newAjaxResult(); 

      try 

      { 

        //创建存放静态页面目录               

        if(!Directory.Exists(Path.GetDirectoryName(htmlPath))) 

        { 

          Directory.CreateDirectory(Path.GetDirectoryName(htmlPath)); 

        } 

        //删除已有的静态页面 

        if(File.Exists(htmlPath)) 

        { 

          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; 

        } 

   

        /* 

         * 设置临时数据字典作为静态化标识 

         * 可以在视图上使用TempData["IsStatic"]来控制某些元素显示。 

         */ 

        if(!context.Controller.TempData.ContainsKey("IsStatic")) 

        { 

          context.Controller.TempData.Add("IsStatic",true); 

        } 

   

        if(result.View !=null) 

        { 

          using(var sw =newStringWriter()) 

          { 

            var viewContext =newViewContext(context, 

                             result.View, 

                             context.Controller.ViewData, 

                             context.Controller.TempData, sw); 

   

            result.View.Render(viewContext, sw); 

   

            stringbody = sw.ToString(); 

            File.WriteAllText(htmlPath, body, Encoding.UTF8); 

            ajaxResult.IsSucess =true; 

            ajaxResult.Body ="存放路径:"+ htmlPath; 

          } 

        } 

        else 

        { 

          ajaxResult.IsSucess =false; 

          ajaxResult.Body ="生成静态页面失败!未找到视图!"; 

        } 

      } 

      catch(IOException ex) 

      { 

        ajaxResult.IsSucess =false; 

        ajaxResult.Body = ex.Message; 

      } 

      catch(Exception ex) 

      { 

        ajaxResult.IsSucess =false; 

        ajaxResult.Body = ex.Message; 

      } 

      returnajaxResult; 

    } 

AjaxResult 是自己封装的一个类,您也可以用自己封装的类代替。

 

publicclassAjaxResult

{

  publicboolIsSucess {get;set; }

  publicstringBody {get;set; }

}

原文链接:http://blog.csdn.net/a497785609/article/details/51354851

【asp.net core】ASP.NET MVC 利用Razor引擎生成静态页

http://m.bbyears.com/asp/124531.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新