Home > Backend Development > C#.Net Tutorial > Detailed example of using Razor engine to generate static pages (ASP.NET MVC)

Detailed example of using Razor engine to generate static pages (ASP.NET MVC)

Y2J
Release: 2017-04-25 13:43:32
Original
1741 people have browsed it

I have recently been studying ASP.NET MVC to generate static pages, so today can be considered a study note!

Implementation principles and steps:

1. Find the corresponding view through ViewEngines.Engines.FindView. If it is a partial view, use: ViewEngines.Engines.FindPartialView;

2. Set the Model in the context object;

3. Call the Render() method of the view and save the rendering results to the physical static file;

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; 
    } 
  } 
}
Copy after login

AjaxResult is encapsulated by itself A class, you can also replace it with your own encapsulated class.

  public class AjaxResult
  {
    public bool IsSucess { get; set; }
    public string Body { get; set; }
  }
Copy after login

The above is the detailed content of Detailed example of using Razor engine to generate static pages (ASP.NET MVC). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template