.netcore 一个mvc到静态html实现
一、新建Mvc项目
Program.cs添加拦截
二、添加一个集成测试
将页面转为html到wwwroot下面
UnitGenHtml.cs
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.VisualStudio.TestPlatform.TestHost;namespace SaaS.OfficialWebSite.Test
{public class UnitGenHtml : IClassFixture<WebApplicationFactory<Program>>{private readonly WebApplicationFactory<Program> _factory;private const string RootDir = "D:\\xxx\\wwwroot";public UnitGenHtml(WebApplicationFactory<Program> factory){_factory = factory;}[Theory][InlineData("/Ocr/")][InlineData("/Bar/")][InlineData("/Cer/")][InlineData("/Pwd/")][InlineData("/Diff/")][InlineData("/Encry/")][InlineData("/ExcelToJson/")][InlineData("/ExcelToSql/")][InlineData("/Json/")][InlineData("/Jwt/")][InlineData("/Reg/")][InlineData("/SqlFormat/")][InlineData("/Unix/")][InlineData("/UrlCode/")][InlineData("/Home/")]public async Task GenHtml(string url){var client = _factory.CreateClient();var response = await client.GetAsync(url);if (response.EnsureSuccessStatusCode().IsSuccessStatusCode){if (url == "/Home/"){url = "index";}var htmlFile = System.IO.Path.Combine(RootDir, url.Replace("/", "") + ".html");if (System.IO.File.Exists(htmlFile)){System.IO.File.Delete(htmlFile);}var html = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();html = ReplaceHtmlA(html);System.IO.File.WriteAllText(htmlFile, html, System.Text.Encoding.UTF8);}}private string ReplaceHtmlA(string htmlFile){var pages = new List<string>{"", "Ocr","Bar","Pwd", "Cer", "Diff","Encry","ExcelToJson","ExcelToSql","Json","Jwt","Reg","SqlFormat","Unix","UrlCode"};foreach (var page in pages){htmlFile = htmlFile.Replace($"href=\"/{page}\"", $"href=\"/{(page == "" ? "index" : page)}.html\"");}return htmlFile;}}
}
实现效果:OCR图片识别工具 - 在线提取图片中的文字 | 免费无注册