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

Gol*_*old 34 c# asp.net

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

在asp.net中

提前致谢

Dar*_*rov 37

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

您还可以使用HostingEnvironment.ApplicationPhysicalPath属性.

  • 如果您没有可用请求,请使用此选项,就像我没有请求一样.很好的答案. (3认同)

hun*_*ter 30

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

Request.ApplicationPath;
Run Code Online (Sandbox Code Playgroud)

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

ResolveUrl("~/bin");
Run Code Online (Sandbox Code Playgroud)


jen*_*ent 16

我需要在这app_start那里是没有的HttpContext,因此RequestServer没有选择.

这样就可以了:

System.Web.HttpRuntime.BinDirectory
Run Code Online (Sandbox Code Playgroud)


Raj*_*mar 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;
        }
}
Run Code Online (Sandbox Code Playgroud)

以及博客中描述的更多内容


Sut*_*rth 13

使用以下代码段:

string strPath = HttpContext.Current.Server.MapPath("~/bin");
Run Code Online (Sandbox Code Playgroud)