获取asp.net mvc中的当前目录
我正在尝试构建一个文件路径以读取 XSLT 文件,如下所示:
string path = "../_xslt/example.xslt";
StreamReader reader = new StreamReader(path);
...我位于控制器 (/Controllers/ExampleController.cs) 中,并且“/_xslt/”文件夹位于同一级别as '/Controllers'
但是,我收到的错误是:
(系统.IO.DirectoryNotFoundException) 找不到路径“c:\windows\system32\_xslt\example.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);
...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)
Could not find a part of the path 'c:\windows\system32\_xslt\example.xslt'.
Am I going about this the wrong way?
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 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
:如果控制器存在于目录根目录
否则
If controller is present at directory root
Else