【问题标题】:how to get the application path in asp.net?如何在asp.net中获取应用程序路径?
【发布时间】:2011-05-16 23:00:34
【问题描述】:

如何获取应用路径?文件夹路径

在 asp.net 中

先谢谢了

【问题讨论】:

标签: c# asp.net


【解决方案1】:
Server.MapPath("~/bin")

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

【讨论】:

  • 如果您没有可用的请求,请使用它,就像我没有一样。很好的答案。
【解决方案2】:

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

Request.ApplicationPath;

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

ResolveUrl("~/bin");

【讨论】:

    【解决方案3】:

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

    这成功了:

    System.Web.HttpRuntime.BinDirectory
    

    编辑

    .net core 开始,您可以使用 nuget pkg Microsoft.Extensions.PlatformAbstractionsPlatformServices.Default.Application.ApplicationBasePath,它可以解析任何运行时。

    【讨论】:

      【解决方案4】:
      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 中描述的更多内容

      【讨论】:

      • 这是我可以在 WebApi 请求中有效使用的唯一答案。谢谢
      【解决方案5】:

      使用以下sn-p:

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

      【讨论】:

        【解决方案6】:
        Server.MapPath("~/bin")
        

        你也可以使用 Request.PhysicalApplicationPath

        【讨论】:

          【解决方案7】:

          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

          【讨论】:

          • 请在您的答案中添加一些解释,以便其他人可以从中学习
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-25
          • 2014-11-22
          • 1970-01-01
          • 2012-05-23
          • 2010-10-30
          相关资源
          最近更新 更多