欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解ASP.NET MVC 利用Razor引擎生成静态页

程序员文章站 2023-10-21 22:14:04
最近在研究asp.net mvc生成静态页,那么今天也算个学习笔记吧! 实现原理及步骤: 1.通过viewengines.engines.findview查找到对应...

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

实现原理及步骤:

1.通过viewengines.engines.findview查找到对应的视图,如果是部分视图,则用:viewengines.engines.findpartialview;

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

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

using system; 
using system.io; 
using system.text; 
using system.web.mvc; 
 
namespace whir.foundation.ui 
{ 
  /// <summary> 
  ///   描述:静态页面生成帮助类 
  /// </summary> 
  public class staticpagehelper 
  { 
    /// <summary> 
    ///   根据view视图生成静态页面 
    /// </summary> 
    /// <param name="htmlpath">存放静态页面所在绝对路径</param> 
    /// <param name="context">controllercontext</param> 
    /// <param name="viewpath">视图名称</param> 
    /// <param name="mastername">模板视图名称</param> 
    /// <param name="model">参数实体模型</param> 
    /// <param name="html">返回信息</param> 
    /// <param name="ispartial">是否分布视图</param> 
    /// <returns>生成成功返回true,失败false</returns> 
    public static ajaxresult generatestaticpage(string viewpath, 
                          string htmlpath, 
                          controllercontext context, object model = null, bool ispartial = false, 
                          string mastername = "") 
    { 
      var ajaxresult = new ajaxresult(); 
      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 = 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(); 
            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; 
      } 
      return ajaxresult; 
    } 
  } 
} 

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

  public class ajaxresult
  {
    public bool issucess { get; set; }
    public string body { get; set; }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。