【问题标题】:Combining System.IO.Path and System.Uri into a complete system path将 System.IO.Path 和 System.Uri 组合成完整的系统路径
【发布时间】:2014-07-22 14:36:42
【问题描述】:

我正在为我的应用程序编写一个小型 Web 界面,并且正在将已知的文档路径和请求 URI 组合成一个绝对本地路径以从中检索文件。

为简单起见,我将代码范围缩小到相关:

string AssemblyDirectory = "C:\MyAppDir\";
string uri = "/index.html";
return new Uri(new Uri(Path.Combine(AssemblyDirectory, "http_docs")), uri).AbsolutePath;

这将始终只返回 uri 部分;即返回是“/index.html”,它似乎不喜欢正确组合Uri 和Path 的类型。我知道只需用“\”替换“/”然后做一个简单的 path.combine 就可以了,但我不禁认为在我忽略的某个地方有一个 .net 解决方案。

【问题讨论】:

  • 仅供参考。有UriBuilder
  • @DanielA.White 即使 URI 构建器似乎也忽略了任何非 Uri 类型的部分,它仍然只返回 /index.html 部分。

标签: c# path uri


【解决方案1】:

问题是您的index.html 之前有一个/。它使 Uri API 引用 uri 的根,最后为您提供:c:\index.html

删除 uri 中的 / 以使其工作:

string uri = "index.html";

【讨论】:

  • 我觉得自己像个傻瓜,感谢您让我直截了当,在我的路径构建器中添加 uri = uri.TrimStart('/'); 实际上确实解决了所有问题!
猜你喜欢
  • 2014-10-03
  • 1970-01-01
  • 2016-04-15
  • 2013-07-16
  • 2011-02-16
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多