从基础创建 Uri,不带尾部斜杠和相关部分
我的 Uri 构造函数有问题。基本路径是否以斜杠结尾的结果会有所不同。
var baseWithSlash = new Uri("c:\\Temp\\");
var baseNoSlash = new Uri("c:\\Temp");
var relative = "MyApp";
var pathWithSlash = new Uri(baseWithSlash, relative); // file:///c:/Temp/MyApp
var pathNoSlash = new Uri(baseNoSlash, relative); // file:///c:/MyApp
即使基本路径中没有斜杠,第一个结果也是我所期望的。
我的主要问题是基本路径来自用户输入。
即使用户指定不带尾部斜杠的路径,获得正确结果的最佳方法是什么?
I am having a problem with Uri constructor. Results differ on whether base path ends with slash or not.
var baseWithSlash = new Uri("c:\\Temp\\");
var baseNoSlash = new Uri("c:\\Temp");
var relative = "MyApp";
var pathWithSlash = new Uri(baseWithSlash, relative); // file:///c:/Temp/MyApp
var pathNoSlash = new Uri(baseNoSlash, relative); // file:///c:/MyApp
The first result is the one I expect even if there's no slash in base path.
My main problem is that base path comes from user input.
What's the best way to achieve correct result even if user specifies path without trailing slash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
国际海事组织认为这是可以预料的。毕竟,考虑“hello.jpg”的 URI 相对于
是
吗?
现在,如果您知道您的用户正在输入代表目录的 URI,则可以确保该字符串末尾有斜杠。如果您不知道他们是否输入了目录名称,就会出现问题。如果没有斜杠,只添加一个斜杠是否适合您?
这是假设(根据您的示例)他们将使用反斜杠。根据您的具体情况,您可能还需要处理正斜杠。
This is to be expected IMO. After all, consider the URI for "hello.jpg" relative to
It's
right?
Now if you know that your user is entering a URI representing a directory, you can make sure that the string has a slash on the end. The problem comes if you don't know whether they're entering a directory name or not. Will just adding a slash if there isn't one already work for you?
That's assuming (based on your example) that they'll be using backslashes. Depending on your exact circumstance, you may need to handle forward slashes as well.
确保第一部分有一个尾部斜杠(即:检查它)。
Make sure the first part has a trailing slash (i.e: check for it).