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

浪淘沙9个月前后端220

自己在 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))   {...

普能html页面post到 cshtml页面,出现400错误解决方法

  [IgnoreAntiforgeryToken]  public class keywordsModel : PageModel  {    &n...

asp.net core postgresql 大数据高性能分页

 /// <summary> /// 分页:返回json格式,参数加密 /// </summary> /// <param nam...

asp.net core webapi 调用furion 的webapi json接口

/// <summary> /// 根据学校读取支付科次 /// </summary> /// <returns>&l...

SqlSugar

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

asp.net core razor OnGet 异步写法

public async Task<IActionResult> OnGetAsync() {     var&n...

发表评论    

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