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

浪淘沙4个月前后端124

自己在 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();


相关文章

echarts获取点击事件

  myChart.on('click', (params) => {      // 检查是否点击了系列数据   ...

asp.net core Aspose.Words 文本换行

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

asp.net core furion 返回错误信息

asp.net core furion 返回错误信息

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

asp.net core sqlsugar 复制插入并返回新id

var result = await _db.Ado.GetScalarAsync("insert into qy_gw(gwmc...

asp.net razor post

asp.net razor post

@page @model xxtsoft.Web.Entry.Pages.qd.IndexModel @{     Layout =&...

常用查询

 var dt = _db.SqlQueryable<object>("select * from mszs...

发表评论    

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