一个文件搞定动态页面转静态页面,支持.NET CORE 3.1 ,支持MVC和PageModel。
One file gets dynamic pages to static pages, supports .NET CORE 3.1, and supports MVC and PageModel.
1、将HtmlStaticFileAttribute.cs
放到项目下;
Put HtmlStaticFileAttribute.cs
under the project;
2、在控制器文件中,添加命名空间Microsoft.AspNetCore.Mvc
;
In the controller file, add the namespace Microsoft.AspNetCore.Mvc
;
3、在类名
或Action
方法上添加[HtmlStaticFile]
。
Add [HtmlStaticFile]
to the class name
or Action
method.
相关如下代码(Related code):
using Microsoft.AspNetCore.Mvc;
namespace StaticPage.Mvc.Controllers
{
public class HomeController : Controller
{
[HtmlStaticFile]
[HttpGet("/Count")]
public IActionResult Count()
{
c++;
ViewBag.C = c;
return View();
}
}
}
1、将HtmlStaticFileAttribute.cs
放到项目下;
Put HtmlStaticFileAttribute.cs
under the project;
2、在PageModel文件中,添加命名空间Microsoft.AspNetCore.Mvc
。
In the PageModel file, add the namespace Microsoft.AspNetCore.Mvc
.
3、在类名上添加[HtmlStaticFile]
,注:方法上添加[HtmlStaticFile]
是无效的。
Add [HtmlStaticFile]
to the class name. Note: Adding [[HtmlStaticFile] `to the method is invalid.
相关如下代码(Related code):
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;
在开发模式,页面不会被缓存,便于开发调试。
In the development mode, the page will not be cached, which is convenient for development and debugging.
HtmlStaticFileAttribute.IsDevelopmentMode = true;
HtmlStaticFileAttribute.UseQueryString = true;
推荐使用WebMarkupMin
来压缩Html。
It is recommended to use WebMarkupMin
to compress 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”,访问一下就可以生成新的静态页面。
Add the parameter "update" after the Url address, and you can generate a new static page after visiting it.
https://localhost:44304/Count?__update__
在Url地址后面添加参数“test”,访问一下就可以看到最新页面。
Add the parameter "test" after the Url address, you can see the latest page after visiting.
https://localhost:44304/Count?__test__
如果这个类库有帮助到您,请 Star 这个仓库。
If this class library helps you, please ask Star this repository.
你也可以选择使用支付宝或微信给我捐赠:
You can also choose to use Alipay or WeChat to donate to me: