• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

ASP.NET MVC生成静态页面的方法

asp 搞代码 4年前 (2022-01-03) 35次浏览 已收录 0个评论

对于网站来说,生成纯html静态页面除了有利于seo外,还可以减轻网站的负载能力和提高网站性能。本篇文章主要介绍了ASP.NET MVC生成静态页面的方法,有兴趣的可以了解一下。

本文主要讲述了在asp.NET mvc中,页面静态化的方法。对于网站来说,生成纯html静态页面除了有利于seo外,还可以减轻网站的负载能力和提高网站性能。

1.先付上封装好生成静态页的原代码:

 public class Common { #region 获取模板页的Html代码 /// <summary> /// 获取页面的Html代码 /// </summary> /// 模板页面路径 /// 页面编码 ///  public static string GetHtml(string url, System.Text.Encoding encoding) { byte[] buf = new WebClient().DownloadData(url); if (encoding != null) { return encoding.GetString(buf); } string html = System.Text.Encoding.UTF8.GetString(buf); encoding = GetEncoding(html); if (encoding == null || encoding == System.Text.Encoding.UTF8) { return html; } return encoding.GetString(buf); } /// <summary> /// 获取页面的编码 /// </summary> /// Html源码 ///  public static System.Text.Encoding GetEncoding(string html) { string pattern = @"(?i)\bcharset=(?[-a-zA-Z_0-9]+)"; string charset = Regex.Match(html, pattern).Groups["charset"].Value; try { return System.Text.Encoding.GetEncoding(charset); } catch (ArgumentException) { return null; } } #endregion #region 用于生成Html静态页 /// <summary> /// 创建静态文件 /// </summary> /// Html代码 /// 生成路径 ///  public static bool CreateFileHtmlByTemp(string result, string createpath) { if (!string.IsNullOrEmpty(result)) { if (string.IsNullOrEmpty(createpath)) { createpath = "/default.html"; } string filepath = createpath.Substring(createpath.LastIndexOf(@"\")); createpath = createpath.Substring(0, createpath.LastIndexOf(@"\")); if (!Directory.Exists(createpath)) { Directory.CreateDirectory(createpath); } createpath = createpath + filepath; try { FileStream fs2 = new FileStream(createpath, FileMode.Create); StreamWriter sw = new StreamWriter(fs2, new System.Text.UTF8Encoding(false));//去除UTF-8 BOM sw.Write(result); sw.Close(); fs2.Close(); fs2.Dispose(); return true; } catch (Exception ex) { throw ex; } } return false; } #endregion #region 调用静态模板,并且传递数据模型实体类 创建Html静态页 /// <summary> /// 解析模板生成静态页 /// </summary> /// 模板地址 /// 静态页地址 /// 数据模型 ///  public static bool CreateStaticPage(string temppath, string path, T t) { try { //获取模板Html string TemplateContent = GetHtml(temppath, System.Text.Encoding.UTF8); //初始化结果 string result = string.Empty; //解析模板生成静态页Html代码 result = Razor.Parse(TemplateContent, t); //创建静态文件 return CreateFileHtmlByTemp(result, path); } catch (Exception e) { throw e; } } #endregion } 

2.调用方法(创建一个多线程去执行,效果会更好):

 //实例化调用方法 Task tk = new Task(CreateStaticHtml); tk.Start(); //静态调用方法 Task.Factory.StartNew(() => CreateStaticHtml());

3.封装好的静态方法:

 /// <summary> /// 创建静态页面 /// </summary> public void CreateStaticHtml() { using (BangLiEntities bangLi = new BangLiEntities()) { View_Home view_Home = new View_Home(); view_Home.CommandAdExtendList = Dal.CommandAdDal.Instance(bangLi).GetInit().ToList(); view_Home.NewsList = bangLi.News.OrderByDescending(u => u.AddDateTime).Take(5).ToList(); view_Home.NewsExtendList = Dal.NewsDal.Instance(bangLi).GetInit().OrderByDescending(u => u.AddDateTime).Take(5).ToList(); view_Home.News = Dal.NewsDal.Instance(bangLi).GetInit().Where(u => u.Id == 3).SingleOrDefault(); string TemplateContent = Common.GetHtml(Server.MapPath("/Views/SourceHtml/Home/Index.cshtml"), System.Text.Encoding.UTF8); //初始化结果 string result = string.Empty; //解析模板生成静态页Html代码 result = Razor.Parse(TemplateC<strong style="color:transparent">来源gaodai#ma#com搞@@代~&码网</strong>ontent, view_Home); //创建静态文件 Common.CreateFileHtmlByTemp(result, Server.MapPath("/Resource/Manage/Html/Home/Index.html")); } } 

4.如首页执行时,可以在执行Action前去执行一个过滤器:

 public class MyFirstHomeAttribute:ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var context = filterContext.HttpContext; context.Session["IsStaticHtml"] = false; string path = context.Server.MapPath("/Resource/Manage/Html/Home/Index.html"); if (System.IO.File.Exists(path)) { string html = System.IO.File.ReadAllText(path); context.Response.Write(html); context.Session["IsStaticHtml"] = true; context.Response.End(); } } } 

5.执行首页:

 [MyFirstHome] public ActionResult Index() { View_Home view_Home = null; var IsStaticHtml = Convert.ToBoolean(Session["IsStaticHtml"]); if (!IsStaticHtml) { view_Home = new View_Home(); using (BangLiEntities bangLi = new BangLiEntities()) { view_Home.CommandAdExtendList = Dal.CommandAdDal.Instance(bangLi).GetInit().ToList(); view_Home.NewsExtendList = Dal.NewsDal.Instance(bangLi).GetInit().OrderByDescending(u => u.AddDateTime).Take(5).ToList(); } return View(view_Home); } else { return null; } } 

说明:可以让一个超链接或跳转地址直接跳转到一个html的静态页面,速度会更快;

以上就是ASP.NET MVC生成静态页面的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ASP.NET MVC生成静态页面的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址