Asp. net core 3.1 How does a file easily turn a dynamic page to a static page
- 2021-11-14 05:15:58
- OfStack
Preface
Recently, a static page was needed for an Asp. net core project. Baidu searched for one time and found no suitable one. The reasons are as follows
Troublesome configuration. The class library refers to the third class, which is troublesome to modify. Only MVC is supported, but PageModel is not supported. Inherit ActionFilterAttribute class, only rewrite OnActionExecutionAsync, seemingly static, in fact, the run time to check the database or database, not really static. Lack of flexibility, no way to update static files online, no way to test and view real-time pages, no Html compression, no use of gzip, br compressed files.So I started the page static project, and in just a few minutes, I encountered a big hole in Asp. net core-Response. Body was a write-only Stream, and could not read the returned information.
Refer to the blog of lwqlun, and the related address is https://www.ofstack.com/article/187210. htm
The code is as follows:
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;
}
After solving this big pit, there was no problem.
Project address: https://github.com/toolgood/StaticPage
Quick Start
1. Put HtmlStaticFileAttribute. cs under the project;
2. Add [HtmlStaticFile]
2.1 In the controller file, add [HtmlStaticFile] to the class name or Action method.
using Microsoft.AspNetCore.Mvc;
namespace StaticPage.Mvc.Controllers
{
public class HomeController : Controller
{
[HtmlStaticFile]
[HttpGet("/Count")]
public IActionResult Count()
{
return View();
}
}
}
2.2 Or in the PageModel file, add [HtmlStaticFile] to the class name.
Note: In the PageModel file, adding [HtmlStaticFile] to the method is invalid.
using Microsoft.AspNetCore.Mvc;
namespace StaticPage.Pages
{
[HtmlStaticFile]
public class CountModel : PageModel
{
public void OnGet()
{
}
}
}
Other configurations
Set the cache folder
HtmlStaticFileAttribute. OutputFolder = @ "D:\ html";
Use compression
HtmlStaticFileAttribute. UseBrCompress = true;
HtmlStaticFileAttribute. UseGzipCompress = true;
Set the page cache time
HtmlStaticFileAttribute. ExpireMinutes = 3;
Use development mode, in development mode, pages will not be cached, which is convenient for development and debugging.
HtmlStaticFileAttribute. IsDevelopmentMode = true;
Support Url parameter, not recommended
HtmlStaticFileAttribute. UseQueryString = true;
Using Html compression, WebMarkupMin is recommended for compression of 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;
};
Update file cache
Add the parameter "update" after the Url address, and visit 1 to generate a new static page.
Such as:
https://localhost: 44304/Count? __update__
Test the page without updating the file cache
Add the parameter "test" after the Url address, and visit 1 to generate a new static page.
Such as:
https://localhost: 44304/Count? __test__
Project address: https://github.com/toolgood/StaticPage
Summarize