获取asp.net mvc的当前目录 [英] Get current directory in asp.net mvc

查看:369
本文介绍了获取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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆