.net WebApi - HttpContext.Current.Server.MapPath的错误值

Guy*_*ski 3 .net c# asp.net-web-api visual-studio-2012

我正在使用HttpContext.Current.Server.MapPath函数来查找IIS托管的站点中的资源.

HttpContext.Current.Server.MapPath("localPath")
Run Code Online (Sandbox Code Playgroud)

我从中获得的价值是:

c:\users\guy\documents\visual studio 2012\Projects\MyProject\MyProject\api\localPath

而真正的内容是在

C:\Users\guy\Documents\Visual Studio 2012\Projects\MyProject\MyProject\localPath

为什么我api最终得到一个额外的?也许这与我运行webapi应用程序的事实有关?

谢谢.

Max*_*akh 7

您可能需要添加~/以获取根文件夹.像这样:

HttpContext.Current.Server.MapPath("~/") 
Run Code Online (Sandbox Code Playgroud)