我正在嘗試獲取托管在 IIS 中的 ASP.NET 6 應用程式的名稱。我需要的正是這個帶有適當大小寫的名稱:

在 .NET Framework 4.8 中,此名稱由HttpRequest.ApplicationPath提供,并以正確的大小寫形式回傳(與 IIS 中的配置相同,而不是在即將到來的請求的 URL 中)。但是,它在 .NET 6 中不存在。
我試過了:
HttpContext.Request.PathBase, 但它回傳的路徑與請求 URL 中的路徑完全相同,而不是 IIS 中的路徑- 注入
IServerAddressesFeatureandIWebHostEnvironment,但它們都不包含來自 IIS 且大小寫正確的名稱 IServerAddressesFeature,但在這里也沒有找到任何相關的東西- 獲取服務器變數:
IServerVariablesFeature serverVars = HttpContext.Features.Get<IServerVariablesFeature>()然后是 IIS 站點名稱:(請參閱此處string iis_version = serverVars["INSTANCE_NAME"]的檔案),但它以大寫字母 ( )回傳應用程式名稱MYSITE.WEB
有誰知道如何獲取在 IIS 中配置的該站點的名稱(使用正確的大小寫)?
uj5u.com熱心網友回復:
TL;博士:
- 使用
APPL_MD_PATH服務器變數。- 這包含 IIS 元資料庫應用程式路徑字串。(AFAIK,“MD”代表“元資料庫”)。
- 參考檔案:
https://docs.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms524602(v=vs.90)?redirectedfrom=MSDN
"
APPL_MD_PATH- 檢索應用程式的元資料庫路徑。"
像這樣:
// This code assumes HttpContext is available, such as in a Middleware method or `Controller` subclass.
using Microsoft.AspNetCore.Http;
String? iisMetabasePath = httpContext.GetServerVariable("APPL_MD_PATH");
// or (long-form):
String? iisMetabasePath = HttpContextServerVariableExtensions.GetServerVariable( httpContext, "APPL_MD_PATH" );
然后只需修剪/LM/W3SVC/部分。
請注意,當您在 IIS 之外運行代碼時,例如使用 ASP.NET Core 的開發服務器,所有 IIS 特定的資料(例如)"APPL_MD_PATH"都將不可用,因此請確保您也在處理這種情況。
原始研究:發生了ApplicationRoot什么?
是時候淘汰ILSpy了……
HttpRequest.ApplicationPath是HttpRuntime.AppDomainAppVirtualPath。HttpRuntime.AppDomainAppVirtualPath是VirtualPath.GetVirtualPathStringNoTrailingSlash(HttpRuntime._theRuntime._appDomainAppVPath)。HttpRuntime._theRuntime._appDomainAppVPath設定在HttpRuntime.Init().HttpRuntime.Init()從._appDomainAppVPath_HttpRuntime.GetAppDomainString(".appVPath"))- “AppDomain 字串”是與每個
AppDomain. - 一般來說,.NET Framework (aka ) 中的 ASP.NET 為IIS中的每個應用程式范圍
System.Web創建一個新的。AppDomain - And of course, AppDomains no-longer exist in .NET Core and later.
- So let's find out where the
Stringvalue for".appVPath"comes from...
- “AppDomain 字串”是與每個
System.Web.Hosting.ApplicationManager::PopulateDomainBindingssetsdict.Add(".appVPath", appVPath.VirtualPathString)- "domain bindings" in this context refers to AppDomain bindings: nothing at all to do with DNS domain-names or
Hostheader bindings in IIS. Yay for overloaded terminology.
- "domain bindings" in this context refers to AppDomain bindings: nothing at all to do with DNS domain-names or
PopulateDomainBindingsis called bySystem.Web.Hosting.ApplicationManager::CreateAppDomainWithHostingEnvironment.- And it gets
virtualPath: VirtualPath.Create(appHost.GetVirtualPath()).
- And it gets
appHost.GetVirtualPath()isIApplicationHost.GetVirtualPath().- There are 2 in-box implementations:
System.Web.Hosting.ISAPIApplicationHostandSystem.Web.Hosting.SimpleApplicationHost. We're interested inISAPIApplicationHost.
- There are 2 in-box implementations:
ISAPIApplicationHostgets its virtualPath from the runtime argument toString appIdandString appPathin theIAppManagerAppDomainFactory.Createmethod.- And
IAppManagerAppDomainFactoryis a COM interface used directly by IIS.- The plot thickens...
- And
- At this point I got lost trawling through the legacy IIS 6 ISAPI documentation, looking for traces of the original COM definition of
IAppManagerAppDomainFactorybut came up empty-handed.- It's probably handled by
webengine4.dllwhich is a native DLL and I don't have the time to bust-out Ghidra right now... - I did notice that the ISAPI request entrypoint method
HttpExtensionProc(and itsLPEXTENSION_CONTROL_BLOCKparameter) do not contain the IIS Application ScopeAppIdor Virtual Path, which surprised me - but most importantly: this suggests the value must likely come from theGetServerVariableorServerSupportFunctioncallbacks.... - However that's likely a waste of time anyway IIS 6 is not IIS7 and IIS7 's interfaces are no-longer called "ISAPI" but instead called just "IIS Native-Code API" (that's the most plain and boring API name I've seen in a while...).
- It's probably handled by
- So, starting with the "IIS Native-Code API" documentation I quickly found the
IWpfApplicationInfoUtil::GetApplicationPropertiesFromAppIdmethod (here "WPF" means "Worker Process Framework", and is entirely unrelated to the other UI-related WPF).- I also found the same data exposed via the
IMetadataInfo.GetMetaPath()method (which returns a string of the form"LM/WEBROOT/AppHost/{SiteId}"). - So how does an application get a
IWpfApplicationInfoUtilinterface reference?IWorkerProcessFramework->GetWpfInterface(WPF_APPLICATION_INFO_UTIL_ID)->GetApplicationPropertiesFromAppId- So how do you get
IWorkerProcessFramework?- It's passed as a parameter into your
w3wp.exeworker DLL. - So let's see what
AspNetCoreModuleV2does...
- It's passed as a parameter into your
- So how do you get
- I also found the same data exposed via the
- Actually
AspNetCoreModuleV2uses IIS'sIHttpApplication, derp.IHttpApplicationexposesGetAppConfigPath()which also returns a string of the form/LM/W3SVC/1/ROOT/{Site Name}.- ASP.NET Core seems to use it in a few places.
- At this point I gave up as it's now 6:20am, but that was a fun dive!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/453991.html
標籤:C# 网 asp.net 核心 iis .net-6.0
上一篇:UploadFromByteArrayAsync未在asp.net核心的AzureBlob存盤中保存帶有檔案名的擴展名
下一篇:如何將導航欄大小修復為原始大小
