visual studio 配置 asp.net core razor .shtml文件类型和包含,支持跨平台通用

浪淘沙7个月前后端196

自己在 ASP.NET Core 里写 Middleware 解析 SSI

自己在 ASP.NET Core 里写 Middleware 解析 SSI
// 先处理 .shtml 文件(SSI)
app.Use(async (context, next) =>
{
    var path = context.Request.Path.Value;

    if (!string.IsNullOrEmpty(path) && path.EndsWith(".shtml", StringComparison.OrdinalIgnoreCase))
    {
        var filePath = Path.Combine(env.WebRootPath, path.TrimStart('/'));

        if (File.Exists(filePath))
        {
            var content = await File.ReadAllTextAsync(filePath);

            // 处理 <!--#include file="xxx.html" -->
            var regex = new Regex(@"<!--#include\s+file=""([^""]+)""\s*-->", RegexOptions.IgnoreCase);

            content = regex.Replace(content, match =>
            {
                var includeFile = Path.Combine(Path.GetDirectoryName(filePath)!, match.Groups[1].Value);

                if (File.Exists(includeFile))
                {
                    return File.ReadAllText(includeFile);
                }
                else
                {
                    return $"<!-- include {match.Groups[1].Value} not found -->";
                }
            });

            context.Response.ContentType = "text/html; charset=utf-8";
            await context.Response.WriteAsync(content);
            return; // ⚠️ 一定要 return,避免走到 UseStaticFiles
        }
    }

    await next();
});

// 其它静态文件(CSS, JS, 图片等)
app.UseStaticFiles();


相关文章

asp.net core furion 返回错误信息

asp.net core furion 返回错误信息

  if (string.IsNullOrEmpty(jd) || string.IsNullOrEmpty(wd))   {...

asp.net core Aspose.Words 文本换行

using Aspose.Words;// 替换换行符为 Word 中的换行符var placeholders = new&nbs...

asp.net core razor 写入读取cookie集合

@{     Layout = null; } <form method="post">...

asp.net core SqlSugar 多库切换

  private readonly SqlSugar.ISqlSugarClient _db;     ...

DBeaver连接oracle 下载驱动报错

https://blog.csdn.net/weixin_45764765/article/details/124327194...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。