MVC 3相对路径./bin/

And*_*unt 21 path asp.net-mvc-3

我正在构建一个MVC 3项目,该项目需要通过路径在控制器中引用\bin\位于项目文件夹中的程序集.

有人能指出我正确的方向来构建\bin\文件夹的相对路径吗?

在VS 2010中以调试模式运行项目会导致当前目录C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0,因此使用.\bin\查找bin该路径中的文件夹.

谢谢

小智 45

您可以使用当前的appdomain来获取此信息.

// 1) get current directory for this loaded assembly
// 2) combine path to get \bin folder 
var path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin");
Run Code Online (Sandbox Code Playgroud)


Ric*_*lly 13

由于您在Controller中,因此可以访问HttpServerUtilityBase实例,该实例公开了MapPath方法:

var binDirectoryPath = Server.MapPath("~/bin");
Run Code Online (Sandbox Code Playgroud)

由于MapPath方法是虚拟的并且可以被模拟,因此具有可以单元测试的额外好处.