当前位置: 移动技术网 > IT编程>开发语言>.net > 1个文件如何轻松搞定Asp.net core 3.1动态页面转静态页面

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

2020年06月14日  | 移动技术网IT编程  | 我要评论

温网直播2017,指鹿为马造句,fairuse4wm ibx

前言

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

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

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

参考lwqlun的博客解决了,相关地址:

代码如下:

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]。

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()
    {
    }
  }
}

其他配置

设置缓存文件夹 

  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

总结

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网