为什么 System.Uri 无法识别本地文件路径的查询参数?

发布于 2024-12-25 06:21:54 字数 620 浏览 3 评论 0原文

我需要将一些额外的查询信息添加到文件路径作为查询参数 稍后在文件处理期间解析路径。我虽然 System.Uri 类可以帮助我解决这个问题,但它看起来并没有给出我对本地文件路径的期望。

var fileUri = new Uri("file:///c://a.txt?select=10")
// fileUri.AbsoluteUri = "file:///c://a.txt%3Fselect=10"
// fileUri.Query = ""

var httpUri = new Uri("http://someAddress/a.txt?select=10")
// httpUri.AbsoluteUri = "http://someaddress/a.txt?select=10"
// httpUri.Query = "?select=10"

在“ftp://someAddress/a.txt?select=10”的情况下 - 查询字符串也是空的

我知道 System.Uri 可能会解析“a.txt?select=” 10”来更正文件名“a.txt%3Fselect=10”,但是为什么 - 如何避免这个问题?

提前致谢

I need to add some additional query information to file path as query parameter
to parse path later during files processing. I though that System.Uri class can help me with this, but it looks like it doesn't give me what I expected for local file paths.

var fileUri = new Uri("file:///c://a.txt?select=10")
// fileUri.AbsoluteUri = "file:///c://a.txt%3Fselect=10"
// fileUri.Query = ""

var httpUri = new Uri("http://someAddress/a.txt?select=10")
// httpUri.AbsoluteUri = "http://someaddress/a.txt?select=10"
// httpUri.Query = "?select=10"

In the case of "ftp://someAddress/a.txt?select=10" - query string is also empty

I understand that System.Uri probably resolves "a.txt?select=10" to correct file name "a.txt%3Fselect=10", but WHY - how to escape this?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二手情话 2025-01-01 06:21:54

这是 Microsoft 不会修复的错误: 错误 594562
正如您所看到的,他们提出了反射作为解决方法:

...
Console.WriteLine("Before");
Uri fileUri = new Uri("file://host/path/file?query#fragment");
Console.WriteLine("AbsoluteUri: " + fileUri.AbsoluteUri);
Console.WriteLine("ToString: " + fileUri.ToString());
Console.WriteLine("LocalPath: " + fileUri.LocalPath);
Console.WriteLine("Query: " + fileUri.Query);
Console.WriteLine("Fragment: " + fileUri.Fragment);

Type uriParserType = typeof(UriParser);
FieldInfo fileParserInfo = uriParserType.GetField("FileUri", BindingFlags.Static | BindingFlags.NonPublic);
UriParser fileParser = (UriParser)fileParserInfo.GetValue(null);
FieldInfo fileFlagsInfo = uriParserType.GetField("m_Flags", BindingFlags.NonPublic | BindingFlags.Instance);
int fileFlags = (int)fileFlagsInfo.GetValue(fileParser);
int mayHaveQuery = 0x20;
fileFlags |= mayHaveQuery;
fileFlagsInfo.SetValue(fileParser, fileFlags);

Console.WriteLine();
Console.WriteLine("After");
fileUri = new Uri("file://host/path/file?query#fragment");
Console.WriteLine("AbsoluteUri: " + fileUri.AbsoluteUri);
Console.WriteLine("ToString: " + fileUri.ToString());
Console.WriteLine("LocalPath: " + fileUri.LocalPath);
Console.WriteLine("Query: " + fileUri.Query);
Console.WriteLine("Fragment: " + fileUri.Fragment);  
...  

This is a bug which Microsoft won't fix : Bug 594562
As you can see they propose reflection as an workaround:

...
Console.WriteLine("Before");
Uri fileUri = new Uri("file://host/path/file?query#fragment");
Console.WriteLine("AbsoluteUri: " + fileUri.AbsoluteUri);
Console.WriteLine("ToString: " + fileUri.ToString());
Console.WriteLine("LocalPath: " + fileUri.LocalPath);
Console.WriteLine("Query: " + fileUri.Query);
Console.WriteLine("Fragment: " + fileUri.Fragment);

Type uriParserType = typeof(UriParser);
FieldInfo fileParserInfo = uriParserType.GetField("FileUri", BindingFlags.Static | BindingFlags.NonPublic);
UriParser fileParser = (UriParser)fileParserInfo.GetValue(null);
FieldInfo fileFlagsInfo = uriParserType.GetField("m_Flags", BindingFlags.NonPublic | BindingFlags.Instance);
int fileFlags = (int)fileFlagsInfo.GetValue(fileParser);
int mayHaveQuery = 0x20;
fileFlags |= mayHaveQuery;
fileFlagsInfo.SetValue(fileParser, fileFlags);

Console.WriteLine();
Console.WriteLine("After");
fileUri = new Uri("file://host/path/file?query#fragment");
Console.WriteLine("AbsoluteUri: " + fileUri.AbsoluteUri);
Console.WriteLine("ToString: " + fileUri.ToString());
Console.WriteLine("LocalPath: " + fileUri.LocalPath);
Console.WriteLine("Query: " + fileUri.Query);
Console.WriteLine("Fragment: " + fileUri.Fragment);  
...  
只有影子陪我不离不弃 2025-01-01 06:21:54

请求本地文件时,查询字符串参数无效。

当您使用 http 请求文件时,该文件将被执行,因此能够读取和处理查询字符串。当您请求本地文件时,它不会被执行,因此无法使用查询字符串。

您向文件请求添加查询字符串参数的原因是什么?还有另一种方法吗?

Query string parameters are not valid when requesting a local file.

When you requests a file using http the file is executed and is therefore able to read and process the querystring. When you request a local file it is not executed and so is unable to make use of the querystring.

What is the reason you are adding querystring params to a file request? Is there another way of doing it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文