toolgood

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

最近一个Asp.net core项目需要静态化页面,百度查找了一下,没有发现合适的。原因如下

  1. 配置麻烦。
  2. 类库引用了第三方类,修改起来麻烦。
  3. 有只支持MVC,不支持PageModel。
  4. 继承ActionFilterAttribute类,只重写了OnActionExecutionAsync,看似静态化了,其实运行时该查数据库还是查数据库,没有真正静态化。
  5. 缺少灵活性,没有在线更新静态文件方法,不能测试查看实时页面,没有进行Html压缩,没有使用gzip、br压缩文件.

于是我开始了页面静态化项目,只过几分钟就遇到了Asp.net core的一个大坑——Response.Body是一个只写Stream,无法读取返回的信息。

参考lwqlun的博客解决了,相关地址:https://www.cnblogs.com/lwqlun/p/10954936.html

代码如下:

 1             var filePath = GetOutputFilePath(context);
 2             var response = context.HttpContext.Response;
 3             if (!response.Body.CanRead || !response.Body.CanSeek) {
 4                 using (var ms = new MemoryStream()) {
 5                     var old = response.Body;
 6                     response.Body = ms;
 7 
 8                     await base.OnResultExecutionAsync(context, next);
 9 
10                     if (response.StatusCode == 200) {
11                         await SaveHtmlResult(response.Body, filePath);
12                     }
13                     ms.Position = 0;
14                     await ms.CopyToAsync(old);
15                     response.Body = old;
16                 }
17             } else {
18                 await base.OnResultExecutionAsync(context, next);
19                 var old = response.Body.Position;
20                 if (response.StatusCode == 200) {
21                     await SaveHtmlResult(response.Body, filePath);
22                 }
23                 response.Body.Position = old;
24             }

解决了这个大坑后,就没遇过什么问题了。

项目地址:https://github.com/toolgood/StaticPage

快速入门

1、将HtmlStaticFileAttribute.cs放到项目下;

2、添加[HtmlStaticFile]

2.1、在控制器文件中,在类名Action方法上添加[HtmlStaticFile]

 1 using Microsoft.AspNetCore.Mvc;
 2 
 3 namespace StaticPage.Mvc.Controllers
 4 {
 5     public class HomeController : Controller
 6     {
 7     
 8         [HtmlStaticFile]
 9         [HttpGet("/Count")]
10         public IActionResult Count()
11         {
12             return View();
13         }
14 
15     }
16 }

2.2或 在PageModel文件中,在类名上添加[HtmlStaticFile]。

注:PageModel文件中,在方法上添加[HtmlStaticFile]是无效的。

 1 using Microsoft.AspNetCore.Mvc;
 2 
 3 namespace StaticPage.Pages
 4 {
 5     [HtmlStaticFile]
 6     public class CountModel : PageModel
 7     {
 8         public void OnGet()
 9         {
10         }
11     }
12 }

其他配置

设置缓存文件夹  

  HtmlStaticFileAttribute.OutputFolder = @"D:\html";
使用压缩  

  HtmlStaticFileAttribute.UseBrCompress = true;
  HtmlStaticFileAttribute.UseGzipCompress = true;
设置页面缓存时间  

  HtmlStaticFileAttribute.ExpireMinutes = 3;
使用开发模式 ,在开发模式,页面不会被缓存,便于开发调试。

  HtmlStaticFileAttribute.IsDevelopmentMode = true;
支持Url参数,不推荐使用 

  HtmlStaticFileAttribute.UseQueryString = true;
使用Html压缩,推荐使用WebMarkupMin来压缩Html。

            HtmlStaticFileAttribute.MiniFunc += (string html) => {
                var js = new NUglifyJsMinifier();
                var css = new NUglifyCssMinifier();

                XhtmlMinifier htmlMinifier = new XhtmlMinifier(null, css, js, null);
                var result = htmlMinifier.Minify(html);
                if (result.Errors.Count == 0) {
                    return result.MinifiedContent;
                }
                return html;
            };

更新文件缓存 

  在Url地址后面添加参数“update”,访问一下就可以生成新的静态页面。

如:

   https://localhost:44304/Count?__update__

测试页面,不更新文件缓存 

  在Url地址后面添加参数“test”,访问一下就可以生成新的静态页面。

如:

  https://localhost:44304/Count?__test__

  


 

项目地址:https://github.com/toolgood/StaticPage

 


 

让分享变成常态,这个小项目我花费了3小时左右,如果你使用了这个项目也就等了节省了3小时的研究,

posted on 2020-05-23 10:42  ToolGood  阅读(1696)  评论(1编辑  收藏  举报