C# - 创建新的 URI 对象以指向本地文件

C# - Create new URI object to point to a Local file

如何创建一个 URL 对象指向我的 Web 应用程序的本地文件 我认为这可行:

var myURI = new Uri("~/Content/css/pdfX.css");

但我收到此错误:

Invalid URI: The format of the URI could not be determined.

谢谢

您需要将您拥有的虚拟路径转换为对应的物理文件路径。您可以使用 Server.MapPath 方法来做到这一点。

var myURI = new Uri(Server.MapPath("~/Content/css/pdfX.css"));