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

ASP.NET Core中间件Markdown转换器

目录

需求

文本编码检测

Markdown→HTML

注意

实现


需求

  1. Markdown是一种文本格式;不被浏览器支持;编写一个在服务器端把Markdown转换为HTML的中间件。
  2. 我们开发的中间件是构建在ASP.NET Core内置的StaticFiles中间件之上,并且在它之前运行,所有的*.md文件都被放到wwwroot文件夹下,当我们请求wwwroot下其他的静态文件的时候,StaticFiles中间件会把它们返回给浏览器,而当我们请求wwwroot下的*.md文件的时候,我们编写的中间件会读取对应的*.md文件并且把它们转换为HTML格式返回给浏览器。

文本编码检测

Nuget:Install-Package UTF.Unknown

DetectionResult result = CharsetDetector.DetectFromStream(stream);
string charset = result.Detected.EncodingName

CharsetDetector/UTF-unknown: Character set detector build in C# - .NET 5+, .NET Core 2+, .NET standard 1+ & .NET 4+https://github.com/CharsetDetector/UTF-unknownhttps://github.com/CharsetDetector/UTF-unknownhttps://github.com/CharsetDetector/UTF-unknownhttps://github.com/CharsetDetector/UTF-unknown

Markdown→HTML

Nuget:Install-Package MarkdownSharp

Markdown markdown = new Markdown();
string html = markdown.Transform(mdText);

注意

app.UseMiddleware<MarkdownMiddleware>();需在app.UseStaticFiles();之前注册,如果先注册了静态文件中间件,那么所有对静态文件的请求都会直接由静态文件中间件处理,而不会经过你的自定义中间件。

app.UseMiddleware<MarkdownMiddleware>();
//配置服务器为静态文件提供服务
app.UseStaticFiles();

实现

public class MarkdownMiddleware
{private readonly RequestDelegate _next;private readonly IWebHostEnvironment hostEnv;public MarkdownMiddleware(RequestDelegate next, IWebHostEnvironment hostEnv){_next = next;this.hostEnv = hostEnv;}public async Task InvokeAsync(HttpContext context){//获取请求路径var path = context.Request.Path.Value;//判断请求路径是否以.md结尾if (!path.EndsWith(".md", true, null)){await _next(context);return;}//判断请求路径是否存在var file = hostEnv.WebRootFileProvider.GetFileInfo(path);if (!file.Exists){await _next(context);return;}//读取文件流using var stream = file.CreateReadStream();//UTF.Unknown检测文件编码,获取检测结果DetectionResult result = CharsetDetector.DetectFromStream(stream);string charset = result.Detected.EncodingName ?? "UTF-8";//流的位置重置stream.Position = 0;//读取文件内容,并指定编码using StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(charset));string mdText = await reader.ReadToEndAsync();//将Markdown转换为HTMLMarkdown markdown = new Markdown();string html = markdown.Transform(mdText);//设置响应头context.Response.ContentType = "text/html;charset=UTF-8";await context.Response.WriteAsync(html);}
}
http://www.lryc.cn/news/532644.html

相关文章:

  • 使用page assist浏览器插件结合deepseek-r1 7b本地模型
  • 【华为OD-E卷 - 108 最大矩阵和 100分(python、java、c++、js、c)】
  • 【Reading Notes】Favorite Articles from 2025
  • 云计算行业分析
  • 【Linux系统】线程:线程的优点 / 缺点 / 超线程技术 / 异常 / 用途
  • 3.攻防世界 weak_auth
  • 代码随想录算法训练营| 二叉树总结
  • Python OCR工具pytesseract识别数字验证码
  • SpringBoot开发(五)SpringBoot接收请求参数
  • 文件基础IO
  • 05vue3实战-----配置项目代码规范
  • 八大排序算法细讲
  • 网络爬虫学习:借助DeepSeek完善爬虫软件,增加停止任务功能
  • docker安装es及分词器ik
  • 【论文阅读】On the Security of “VOSA“
  • Docker 国内最新可用镜像源20250205
  • (2025|ICLR,音频 LLM,蒸馏/ALLD,跨模态学习,语音质量评估,MOS)音频 LLM 可作为描述性语音质量评估器
  • 使用 CSS 实现透明效果
  • 4G核心网的演变与创新:从传统到虚拟化的跨越
  • 数据库系统概论的第六版与第五版的区别,附pdf
  • uniapp小程序自定义中间凸起样式底部tabbar
  • 自己实现的一个缓存数据库(搞着玩) .net Core/6/8/9
  • 在Qt中,slots 关键字有什么用?
  • 如何查看linux机器有几个cpu
  • Swoole如何处理内存泄漏
  • Llama最新开源大模型Llama3.1
  • Pixflow - CL-DJI Drone LUTs 120个大疆Drone无人机相机航拍电影级镜头LUT调色预设
  • 了解AI绘图,Stable Diffusion的使用
  • idea整合deepseek实现AI辅助编程
  • llama_index