【发布时间】:2011-05-16 23:00:34
【问题描述】:
如何获取应用路径?文件夹路径
在 asp.net 中
先谢谢了
【问题讨论】:
-
相关帖子here.
如何获取应用路径?文件夹路径
在 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 pkg 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;
}
}
以及blog 中描述的更多内容
【讨论】:
使用以下sn-p:
string strPath = HttpContext.Current.Server.MapPath("~/bin");
【讨论】:
Server.MapPath("~/bin")
你也可以使用
Request.PhysicalApplicationPath
【讨论】:
HostingEnvironment.MapPath() Vs Server.MapPath() Server.MapPath 用于为 asp.net 映射网络服务器上的物理位置。 String path = HttpContext.Current.Server.MapPath("~/myFolder/myFile.txt"); Server.MapPath 指定映射到物理目录的相对或虚拟路径。
样本:
string path=System.Web.Hosting.HostingEnvironment.MapPath(@"~/Files/ExamResult.rdlc");
更多详情请访问Link
【讨论】: