获取asp.net mvc的当前目录 [英] Get current directory in asp.net mvc
问题描述
我想建立一个文件路径,以便读取XSLT文件,像这样:
I am trying to construct a file path in order to read an XSLT file, like so:
string path = "../_xslt/example.xslt";
StreamReader reader = new StreamReader(path);
...这里我在一个控制器(/Controllers/ExampleController.cs),和/ _xslt /文件夹是在同一级别为'/控制器
...where I am in a controller (/Controllers/ExampleController.cs), and the '/_xslt/' folder is at the same level as '/Controllers'
不过,我得到的错误是:
However, the error I am getting is:
(System.IO.DirectoryNotFoundException)
找不到路径的一部分。C:\\ WINDOWS \\ SYSTEM32 \\ _xslt \\ example.xslt
(System.IO.DirectoryNotFoundException) Could not find a part of the path 'c:\windows\system32\_xslt\example.xslt'.
我要对这个错误的方式?
Am I going about this the wrong way?
感谢您的帮助!
推荐答案
您可以使用HttpServerUtility.MapPath方法来映射任何相对路径为你,在你的控制器,这是通过方便的 ControllerContext
:
You can use the HttpServerUtility.MapPath method to map any relative paths for you, in your controller this is easily accessible via the ControllerContext
:
string path = ControllerContext.HttpContext.Server.MapPath("~/_xslt/example.xslt");
...
这篇关于获取asp.net mvc的当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!