Asp.net-Mvc-3

如何在 MVC3 應用程序的 OnApplicationStarted 中獲取應用程序根目錄(完整物理路徑)?

  • October 15, 2019

直到現在我使用:

Dim appPath = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath)

但是今天我將我的應用程序從 IIS6 移動到 IIS7 並且我得到“請求在此上下文中不可用”異常。這是由於 IIS7 中的集成模式管道。我不想進入經典模式,所以我正在尋找一種不同的方式來獲取應用程序路徑(即“C:\SVN\L2E\trunk\Website”)。

有誰知道如何獲得我的 MVC3 應用程序的完整物理路徑?

您可以使用System.Web.HttpRuntime.AppDomainAppVirtualPath獲取虛擬路徑並呼叫System.Web.Hosting.HostingEnvironment.MapPath將虛擬路徑映射到物理路徑。

用這個:

AppDomain.CurrentDomain.BaseDirectory 

引用自:https://stackoverflow.com/questions/16358680