为什么 System.Uri 无法识别本地文件路径的查询参数?
我需要将一些额外的查询信息添加到文件路径作为查询参数 稍后在文件处理期间解析路径。我虽然 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Microsoft 不会修复的错误: 错误 594562
正如您所看到的,他们提出了反射作为解决方法:
This is a bug which Microsoft won't fix : Bug 594562
As you can see they propose reflection as an workaround:
请求本地文件时,查询字符串参数无效。
当您使用 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?