1個(gè)文件如何輕松搞定Asp.net core 3.1動(dòng)態(tài)頁面轉(zhuǎn)靜態(tài)頁面
前言
最近一個(gè)Asp.net core項(xiàng)目需要靜態(tài)化頁面,百度查找了一下,沒有發(fā)現(xiàn)合適的。原因如下
- 配置麻煩。
- 類庫引用了第三方類,修改起來麻煩。
- 有只支持MVC,不支持PageModel。
- 繼承ActionFilterAttribute類,只重寫了OnActionExecutionAsync,看似靜態(tài)化了,其實(shí)運(yùn)行時(shí)該查數(shù)據(jù)庫還是查數(shù)據(jù)庫,沒有真正靜態(tài)化。
- 缺少靈活性,沒有在線更新靜態(tài)文件方法,不能測試查看實(shí)時(shí)頁面,沒有進(jìn)行Html壓縮,沒有使用gzip、br壓縮文件.
于是我開始了頁面靜態(tài)化項(xiàng)目,只過幾分鐘就遇到了Asp.net core的一個(gè)大坑——Response.Body是一個(gè)只寫Stream,無法讀取返回的信息。
參考lwqlun的博客解決了,相關(guān)地址:http://www.dhdzp.com/article/187210.htm
代碼如下:
var filePath = GetOutputFilePath(context);
var response = context.HttpContext.Response;
if (!response.Body.CanRead || !response.Body.CanSeek) {
using (var ms = new MemoryStream()) {
var old = response.Body;
response.Body = ms;
await base.OnResultExecutionAsync(context, next);
if (response.StatusCode == 200) {
await SaveHtmlResult(response.Body, filePath);
}
ms.Position = 0;
await ms.CopyToAsync(old);
response.Body = old;
}
} else {
await base.OnResultExecutionAsync(context, next);
var old = response.Body.Position;
if (response.StatusCode == 200) {
await SaveHtmlResult(response.Body, filePath);
}
response.Body.Position = old;
}
解決了這個(gè)大坑后,就沒遇過什么問題了。
項(xiàng)目地址:https://github.com/toolgood/StaticPage
快速入門
1、將HtmlStaticFileAttribute.cs放到項(xiàng)目下;
2、添加[HtmlStaticFile]
2.1、在控制器文件中,在類名或Action方法上添加[HtmlStaticFile]。
using Microsoft.AspNetCore.Mvc;
namespace StaticPage.Mvc.Controllers
{
public class HomeController : Controller
{
[HtmlStaticFile]
[HttpGet("/Count")]
public IActionResult Count()
{
return View();
}
}
}
2.2或 在PageModel文件中,在類名上添加[HtmlStaticFile]。
注:PageModel文件中,在方法上添加[HtmlStaticFile]是無效的。
using Microsoft.AspNetCore.Mvc;
namespace StaticPage.Pages
{
[HtmlStaticFile]
public class CountModel : PageModel
{
public void OnGet()
{
}
}
}
其他配置
設(shè)置緩存文件夾
HtmlStaticFileAttribute.OutputFolder = @"D:\html";
使用壓縮
HtmlStaticFileAttribute.UseBrCompress = true;
HtmlStaticFileAttribute.UseGzipCompress = true;
設(shè)置頁面緩存時(shí)間
HtmlStaticFileAttribute.ExpireMinutes = 3;
使用開發(fā)模式 ,在開發(fā)模式,頁面不會(huì)被緩存,便于開發(fā)調(diào)試。
HtmlStaticFileAttribute.IsDevelopmentMode = true;
支持Url參數(shù),不推薦使用
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地址后面添加參數(shù)“update”,訪問一下就可以生成新的靜態(tài)頁面。
如:
https://localhost:44304/Count?__update__
測試頁面,不更新文件緩存
在Url地址后面添加參數(shù)“test”,訪問一下就可以生成新的靜態(tài)頁面。
如:
https://localhost:44304/Count?__test__
項(xiàng)目地址:https://github.com/toolgood/StaticPage
總結(jié)
到此這篇關(guān)于1個(gè)文件如何輕松搞定Asp.net core 3.1動(dòng)態(tài)頁面轉(zhuǎn)靜態(tài)頁面的文章就介紹到這了,更多相關(guān)Asp.net core3.1動(dòng)態(tài)頁面轉(zhuǎn)靜態(tài)頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
動(dòng)態(tài)生成table并實(shí)現(xiàn)分頁效果心得分享
動(dòng)態(tài)生成table并實(shí)現(xiàn)分頁在開發(fā)過程中時(shí)一個(gè)很好的應(yīng)用,接下來本文也要實(shí)現(xiàn)一個(gè)類似效果,感興趣的朋友可以參考下哈2013-04-04
ASP.NET?Core中使用Redis實(shí)現(xiàn)緩存
本文詳細(xì)講解了ASP.NET?Core中使用Redis實(shí)現(xiàn)緩存的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
asp.net中強(qiáng)制取消TFS2008中其它成員的簽出文件的方法
有個(gè)項(xiàng)目,以前的成員離職了,剛好又簽出了一個(gè)文件在TFS中并且上了鎖,導(dǎo)致后面的維護(hù)無法簽入和生成。在網(wǎng)上查了一下,找到了如下解決辦法2012-08-08
asp.net下gridview 批量刪除的實(shí)現(xiàn)方法
asp.net下gridview 批量刪除的實(shí)現(xiàn)方法...2007-11-11
ASP.NET?Core擴(kuò)展庫ServiceStack.Redis用法介紹
這篇文章介紹了ASP.NET?Core擴(kuò)展庫ServiceStack.Redis的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02

