![](/img/trans.png)
[英]Is it possible to pass environment variables to an asp .net core application via IIS without saving them to the web.config?
[英]How to set environment variables and use them in IIS application in Azure 2.5
目前,我希望將日志文件放置在特定位置。 有沒有一種方法可以在Global.asax.cs中獲取IIS應用程序的Environment變量
private string GetLoggingPath()
{
string rootFolder = string.Empty;
var directoryInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/")).Parent;
if (directoryInfo != null)
{
if (directoryInfo.Parent != null)
{
rootFolder = directoryInfo.Parent.FullName;
}
}
if (string.IsNullOrWhiteSpace(rootFolder))
{
return Path.GetTempPath();
}
return Path.Combine(rootFolder, @"approot\bin\Data");
}
我一直在研究RoleRoot Environment變量,但是即使應用程序在Azure上運行,它也對我不可用。
如何在Azure中設置環境變量,以便可以在IIS應用程序中使用? 對我來說,最好的方法是在沒有硬編碼的情況下訪問文件夾E:\\approot\\bin\\data
,其中E:\\
是roleroot文件夾
沒有什么好方法,似乎在w3wp.exe進程中未設置環境變量,但是僅使用以下方法:
System.IO.Path.Combine(
System.IO.Path.GetPathRoot(
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath
),
@"approot\bin\Data"
);
(與您幾乎相同,但未創建DirectoryInfo)。
使用Process Explorer查看進程,我可以看到只有IISConfigurator.exe具有RoleRoot環境變量,因此一個選擇是,您可以在啟動任務期間使用AppCmd.exe在某個配置文件中將其設置為web.config或applicationHost。配置,然后從那里讀取。
但老實說,我只會使用上面的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.