如何获取应用程序路径?bin路径
在ASP.NET中
提前感谢
Server.MapPath("~/bin")
获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。
Request.ApplicationPath;
http://msdn.microsoft.com/en-us/library/system.web.httprequest.applicationpath.aspx
ResolveUrl("~/bin");
我在app_start
时需要它,那里还没有HttpContext
,因此Request
和Server
都不是可选项。
这个方法解决了我的问题:
System.Web.HttpRuntime.BinDirectory
编辑
从.net core开始,您可以使用nuget包Microsoft.Extensions.PlatformAbstractions
中的PlatformServices.Default.Application.ApplicationBasePath
,它会为任何运行时解析。
HttpContext.Current.Server.MapPath("~/bin") ;
Application.StartupPath + "/bin";
AppDomain.CurrentDomain.BaseDirectory + "/bin";
//Note in Asp.net Core its little bit different
public class ValuesController : ControllerBase
{
IHostingEnvironment _hostingEnvironment;
public ValuesController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
string applicationPath = _hostingEnvironment.ContentRootPath;
string wwwrootPath = _hostingEnvironment.WebRootPath;
}
}
还有许多其他内容在这个博客中有描述。
使用以下代码片段:
string strPath = HttpContext.Current.Server.MapPath("~/bin");
Server.MapPath("~/bin")
您还可以使用Request.PhysicalApplicationPath
HostingEnvironment.MapPath()
与Server.MapPath()
的区别:Server.MapPath用于将ASP.NET中的物理位置映射到Web服务器上。 String path = HttpContext.Current.Server.MapPath("~/myFolder/myFile.txt");
Server.MapPath指定要映射到物理目录的相对或虚拟路径。
示例:
string path=System.Web.Hosting.HostingEnvironment.MapPath(@"~/Files/ExamResult.rdlc");
更多细节请访问此链接
最初的回答是: