1个文件如何轻松搞定Asp.net core 3.1动态页面转静态页面

2025-05-29 0 64

前言

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

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

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

参考lwqlun的博客解决了,相关地址:http://www.zzvips.com/article/80959.html

代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24
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;

}

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

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

快速入门

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

2、添加[HtmlStaticFile]

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
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]是无效的。

?

1

2

3

4

5

6

7

8

9

10

11

12
using Microsoft.AspNetCore.Mvc;

namespace StaticPage.Pages

{

[HtmlStaticFile]

public class CountModel : PageModel

{

public void OnGet()

{

}

}

}

其他配置

设置缓存文件夹

  HtmlStaticFileAttribute.OutputFolder = @"D:\\html";

使用压缩

  HtmlStaticFileAttribute.UseBrCompress = true;
  HtmlStaticFileAttribute.UseGzipCompress = true;

设置页面缓存时间

  HtmlStaticFileAttribute.ExpireMinutes = 3;

使用开发模式 ,在开发模式,页面不会被缓存,便于开发调试。

  HtmlStaticFileAttribute.IsDevelopmentMode = true;

支持Url参数,不推荐使用

  HtmlStaticFileAttribute.UseQueryString = true;

使用Html压缩,推荐使用WebMarkupMin来压缩Html。

?

1

2

3

4

5

6

7

8

9

10

11
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

总结

到此这篇关于1个文件如何轻松搞定Asp.net core 3.1动态页面静态页面的文章就介绍到这了,更多相关Asp.net core3.1动态页面静态页面内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://www.cnblogs.com/toolgood/p/12941417.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 1个文件如何轻松搞定Asp.net core 3.1动态页面转静态页面 https://www.kuaiidc.com/97023.html

相关文章

发表评论
暂无评论