gpt4 book ai didi

c# - Server.MapPath 和空格

转载 作者:太空狗 更新时间:2023-10-29 23:16:40 26 4
gpt4 key购买 nike

我注意到 Server.MapPath() 中有些奇怪的地方。如果我有一个末尾有空格的文件夹,我会得到:

HttpException: Failed to map the path.

这很好用:Server.MapPath("/Folder1/Folder2/item.jpg")

这很好用:Server.MapPath("/Folder1/Folder2/item.jpg")

这很好用:Server.MapPath("/Folder1/Fol der2/item.jpg")

这失败了!:Server.MapPath("/Folder1/Folder2/item.jpg")

有人可以向我解释为什么末尾的空格会失败,而其他任何地方的空格都不会吗?

注意:所有文件夹都不存在。

最佳答案

因为你shouldn't :

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".

问题来自方法 FileUtil.IsSuspiciousPhysicalPath(string physicalPath, out bool pathTooLong),它进行比较:

string.Compare(physicalPath, Path.GetFullPath(physicalPath), StringComparison.OrdinalIgnoreCase) != 0;

Path.GetFullPath() 将从目录和文件名中删除尾随空格(因为它调用了这样做的 Path.NormalizePath()),可以发现调用Path.GetFullPath(@"C:\Foo\Bar.txt") 例如。由于这与包含空格的原始路径不匹配,该方法将返回 true 从而将路径标识为可疑,之后 Server.MapPath 将抛出异常。

关于c# - Server.MapPath 和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11665179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com