Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate static pages using Razor engine

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

小编给大家分享一下如何利用Razor引擎生成静态页,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

实现原理及步骤:

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 { /// /// 描述:静态页面生成帮助类 /// public class StaticPageHelper { /// /// 根据View视图生成静态页面 /// /// 存放静态页面所在绝对路径 /// ControllerContext /// 视图名称 /// 模板视图名称 /// 参数实体模型 /// 返回信息 /// 是否分布视图 /// 生成成功返回true,失败false 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; } }以上是"如何利用Razor引擎生成静态页"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report