当前位置: 首页 > news >正文

.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图片识别工具 - 在线提取图片中的文字 | 免费无注册

相关文章:

  • 【数据分析】Python+Tushare实现均线金叉死叉交易策略回测
  • 黑马JVM解析笔记(六):深入理解JVM类加载机制与运行时优化
  • 【JS-6.2-模板字符串】ES6 模板字符串:现代JavaScript的字符串处理利器
  • 可达性分析算法Test
  • 如何将Excel表的内容转化为json格式呢?
  • 深入理解Mysql索引底层数据结构和算法
  • InnoDB的redo日志涉及文件及结构
  • 嵌入式硬件中电容的基本原理与详解
  • WPF学习笔记(13)列表框控件ListBox与数据模板
  • Spring Boot项目开发实战销售管理系统——数据库设计!
  • 大模型在恶性心律失常预测及治疗方案制定中的应用研究
  • 计算机网络基础知识详解
  • 筑牢网络安全屏障
  • RabbitMQ 集群与高可用配置
  • 【安全有效新方案】WSL 默认路径迁移实战:通过 PowerShell 符号链接实现自动重定向
  • 【大语言模型入门】—— 浅析LLM基座—Transformer原理
  • 面试八股---HTML
  • 【51单片机节日彩灯控制器设计】2022-6-11
  • Linux的基础IO
  • Segment Anything in High Quality之SAM-HQ论文阅读