如何在asp.net中获取应用程序路径?

问题描述 投票:31回答:7

如何获取应用程序路径? bin路径

在asp.net中

提前致谢

c# asp.net
7个回答
30
投票

获取ASP.NET应用程序在服务器上的虚拟应用程序根路径。

Request.ApplicationPath;

http://msdn.microsoft.com/en-us/library/system.web.httprequest.applicationpath.aspx

ResolveUrl("~/bin");

37
投票
Server.MapPath("~/bin")

您也可以使用HostingEnvironment.ApplicationPhysicalPath属性。


16
投票

我需要这个在app_start,那里还没有HttpContext,因此RequestServer不是选项。

这样就可以了:

System.Web.HttpRuntime.BinDirectory

15
投票
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中描述的更多内容


13
投票

使用以下代码段:

string strPath = HttpContext.Current.Server.MapPath("~/bin");

3
投票
Server.MapPath("~/bin")

你也可以使用Request.PhysicalApplicationPath


1
投票

HostingEnvironment.MapPath()Server.MapPath() Server.MapPath用于在webserver上为asp.net映射物理位置。 String path = HttpContext.Current.Server.MapPath("~/myFolder/myFile.txt"); Server.MapPath指定映射到物理目录的相对或虚拟路径。

样品:

 string path=System.Web.Hosting.HostingEnvironment.MapPath(@"~/Files/ExamResult.rdlc");

详情请访问此Link

© www.soinside.com 2019 - 2024. All rights reserved.