一個檔案搞定Asp.net core 3.1動態頁面轉靜態頁面
最近一個Asp.net core專案需要靜態化頁面,百度查找了一下,沒有發現合適的。原因如下
- 配置麻煩。
- 類庫引用了第三方類,修改起來麻煩。
- 有隻支援MVC,不支援PageModel。
- 繼承ActionFilterAttribute類,只重寫了OnActionExecutionAsync,看似靜態化了,其實執行時該查資料庫還是查資料庫,沒有真正靜態化。
- 缺少靈活性,沒有線上更新靜態檔案方法,不能測試檢視實時頁面,沒有進行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小時的研