让ASP.NET Core服务器(Kestrel)在Windows上区分大小写

11

在 Linux 容器中运行的 ASP.NET Core 应用程序使用区分大小写的文件系统,这意味着 CSS 和 JS 文件的引用必须正确区分大小写。

但是,Windows 文件系统不区分大小写。因此,在开发过程中,您可以引用具有错误大小写的 CSS 和 JS 文件,但它们仍能正常工作。因此,在 Windows 上进行开发时,您将无法知道应用程序在上线 Linux 服务器时是否会出现故障。

有没有办法使 Windows 上的 Kestrel 区分大小写,以便我们可以获得一致的行为,并在上线之前找到引用错误?


澄清一下,Kestrel并不相关,而是PhysicalFileProvider和StaticFiles组件为您执行此匹配。 - Tratcher
3个回答

9

我使用ASP.NET Core中的中间件解决了这个问题。 与标准的app.UseStaticFiles()不同,我使用了:

 if (env.IsDevelopment()) app.UseStaticFilesCaseSensitive();
 else app.UseStaticFiles();

并将该方法定义为:

/// <summary>
/// Enforces case-correct requests on Windows to make it compatible with Linux.
/// </summary>
public static IApplicationBuilder UseStaticFilesCaseSensitive(this IApplicationBuilder app)
{
    var fileOptions = new StaticFileOptions
    {
        OnPrepareResponse = x =>
        {
            if (!x.File.PhysicalPath.AsFile().Exists()) return;
            var requested = x.Context.Request.Path.Value;
            if (requested.IsEmpty()) return;

            var onDisk = x.File.PhysicalPath.AsFile().GetExactFullName().Replace("\\", "/");
            if (!onDisk.EndsWith(requested))
            {
                throw new Exception("The requested file has incorrect casing and will fail on Linux servers." +
                    Environment.NewLine + "Requested:" + requested + Environment.NewLine +
                    "On disk: " + onDisk.Right(requested.Length));
            }
        }
    };

    return app.UseStaticFiles(fileOptions);
}

这也使用了:

public static string GetExactFullName(this FileSystemInfo @this)
{
    var path = @this.FullName;
    if (!File.Exists(path) && !Directory.Exists(path)) return path;

    var asDirectory = new DirectoryInfo(path);
    var parent = asDirectory.Parent;

    if (parent == null) // Drive:
        return asDirectory.Name.ToUpper();

    return Path.Combine(parent.GetExactFullName(), parent.GetFileSystemInfos(asDirectory.Name)[0].Name);
}

你也可以使用自定义的IFileProvider在更低的层次上完成这个操作。 - Tratcher
谢谢老师。你有实现代码吗? - Paymon
@Paymon 请看下面的答案。 - Pierluc SS

6
根据@Tratcher的提议和this博客文章,这里提供了一种解决方案,可以实现对大小写敏感的物理文件提供程序,您可以选择强制大小写敏感或允许任何大小写,而不受操作系统的影响。
public class CaseAwarePhysicalFileProvider : IFileProvider
{
    private readonly PhysicalFileProvider _provider;
    //holds all of the actual paths to the required files
    private static Dictionary<string, string> _paths;

    public bool CaseSensitive { get; set; } = false;

    public CaseAwarePhysicalFileProvider(string root)
    {
        _provider = new PhysicalFileProvider(root);
        _paths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
    }

    public CaseAwarePhysicalFileProvider(string root, ExclusionFilters filters)
    {
        _provider = new PhysicalFileProvider(root, filters);
        _paths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
    }

    public IFileInfo GetFileInfo(string subpath)
    {
        var actualPath = GetActualFilePath(subpath);
        if(CaseSensitive && actualPath != subpath) return new NotFoundFileInfo(subpath);
        return _provider.GetFileInfo(actualPath);
    }

    public IDirectoryContents GetDirectoryContents(string subpath)
    {
        var actualPath = GetActualFilePath(subpath);
        if(CaseSensitive && actualPath != subpath) return NotFoundDirectoryContents.Singleton;
        return _provider.GetDirectoryContents(actualPath);
    }

    public IChangeToken Watch(string filter) => _provider.Watch(filter);

    // Determines (and caches) the actual path for a file
    private string GetActualFilePath(string path)
    {
        // Check if this has already been matched before
        if (_paths.ContainsKey(path)) return _paths[path];

        // Break apart the path and get the root folder to work from
        var currPath = _provider.Root;
        var segments = path.Split(new [] { '/' }, StringSplitOptions.RemoveEmptyEntries);

        // Start stepping up the folders to replace with the correct cased folder name
        for (var i = 0; i < segments.Length; i++)
        {
            var part = segments[i];
            var last = i == segments.Length - 1;

            // Ignore the root
            if (part.Equals("~")) continue;

            // Process the file name if this is the last segment
            part = last ? GetFileName(part, currPath) : GetDirectoryName(part, currPath);

            // If no matches were found, just return the original string
            if (part == null) return path;

            // Update the actualPath with the correct name casing
            currPath = Path.Combine(currPath, part);
            segments[i] = part;
        }

        // Save this path for later use
        var actualPath = string.Join(Path.DirectorySeparatorChar, segments);
        _paths.Add(path, actualPath);
        return actualPath;
    }

    // Searches for a matching file name in the current directory regardless of case
    private static string GetFileName(string part, string folder) =>
        new DirectoryInfo(folder).GetFiles().FirstOrDefault(file => file.Name.Equals(part, StringComparison.OrdinalIgnoreCase))?.Name;

    // Searches for a matching folder in the current directory regardless of case
    private static string GetDirectoryName(string part, string folder) =>
        new DirectoryInfo(folder).GetDirectories().FirstOrDefault(dir => dir.Name.Equals(part, StringComparison.OrdinalIgnoreCase))?.Name;
}

然后在启动类中,确保您按照以下方式为内容和Web根目录注册提供程序:

        _environment.ContentRootFileProvider = new CaseAwarePhysicalFileProvider(_environment.ContentRootPath);
        _environment.WebRootFileProvider = new CaseAwarePhysicalFileProvider(_environment.WebRootPath);

我正在使用 .NET Core 6,并从以下代码中收到错误 System.ArgumentException: 'The path must be absolute. (Parameter 'root')'_provider = new PhysicalFileProvider(root); - Nurkartiko

0

Windows 7中是可能的,但在Windows 10中似乎不行,就我所知,在Windows Server上也完全不可能。

我只能谈论操作系统,因为Kestrel文档说:

UseDirectoryBrowserUseStaticFiles公开的内容的URL受基础文件系统的大小写敏感性和字符限制的影响。例如,Windows不区分大小写,而macOS和Linux则不同。

我建议所有文件名都遵循一个约定(通常使用“全部小写”最好)。为了检查不一致性,您可以运行一个简单的PowerShell脚本,使用正则表达式检查错误的大小写。并且该脚本可以安排定期运行以方便使用。


我们已经有了始终使用小写字母的标准,但很容易犯错误并忽略这个规则。 - Paymon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接