[英]ASP.NET Core 6.0+: How to get the current (case-sensitive) IIS Website Name?
I'm trying to get the name of my ASP.NET 6 app hosted in IIS. What I need is exactly this name with proper casing:我正在尝试获取托管在 IIS 中的 ASP.NET 6 应用程序的名称。我需要的正是这个带有适当大小写的名称:
In .NET Framework 4.8, this name was provided by HttpRequest.ApplicationPath and it was returned with proper casing (as configured in IIS, not as in the coming request's URL).在 .NET Framework 4.8 中,此名称由HttpRequest.ApplicationPath提供,并以适当的大小写返回(如 IIS 中的配置,而不是在即将到来的请求的 URL 中)。 However, it doesn't exist in .NET 6.
但是,它在 .NET 6 中不存在。
I tried:我试过了:
HttpContext.Request.PathBase
, but it returns the path exactly as in the requesting URL, not as in the IIS HttpContext.Request.PathBase
,但它返回的路径与请求 URL 中的路径完全相同,而不是 IIS 中的路径IServerAddressesFeature
and IWebHostEnvironment
, but none of them contains the name from IIS with correct casingIServerAddressesFeature
和IWebHostEnvironment
,但它们都不包含来自 IIS 的名称以及正确的大小写IServerAddressesFeature
, but also didn't find anything relevant here IServerAddressesFeature
,但也没有在此处找到任何相关内容IServerVariablesFeature serverVars = HttpContext.Features.Get<IServerVariablesFeature>()
and then the IIS Site name: string iis_version = serverVars["INSTANCE_NAME"]
(see documentation here ), but it returns the app name in capital letters ( MYSITE.WEB
)IServerVariablesFeature serverVars = HttpContext.Features.Get<IServerVariablesFeature>()
然后是 IIS 站点名称: string iis_version = serverVars["INSTANCE_NAME"]
(参见此处的文档),但它以大写字母返回应用程序名称( MYSITE.WEB
) Does anyone know how to get this site's name as configured in IIS (with proper casing)?有谁知道如何获取在 IIS 中配置的该站点的名称(使用正确的大小写)?
APPL_MD_PATH
server-variable.APPL_MD_PATH
服务器变量。
https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms524602(v=vs.90)?redirectedfrom=MSDN
https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms524602(v=vs.90)?redirectedfrom=MSDN
"
APPL_MD_PATH
- Retrieves the metabase path of the application.""
APPL_MD_PATH
- 检索应用程序的元数据库路径。"
Like so:像这样:
// 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" );
Then just trim-off the /LM/W3SVC/
part.然后只需修剪
/LM/W3SVC/
部分。
Note that when you run your code outside of IIS, such as with ASP.NET Core's development server, all IIS-specific data, like "APPL_MD_PATH"
won't be available, so make sure you're handling that case too.请注意,当您在 IIS 之外运行代码时,例如使用 ASP.NET Core 的开发服务器,所有特定于 IIS 的数据(例如
"APPL_MD_PATH"
将不可用,因此请确保您也在处理这种情况。
ApplicationRoot
?ApplicationRoot
发生了什么? Time to bust-out ILSpy...是时候淘汰ILSpy 了……
HttpRequest.ApplicationPath
is HttpRuntime.AppDomainAppVirtualPath
. HttpRequest.ApplicationPath
是HttpRuntime.AppDomainAppVirtualPath
。HttpRuntime.AppDomainAppVirtualPath
is VirtualPath.GetVirtualPathStringNoTrailingSlash(HttpRuntime._theRuntime._appDomainAppVPath)
. HttpRuntime.AppDomainAppVirtualPath
是VirtualPath.GetVirtualPathStringNoTrailingSlash(HttpRuntime._theRuntime._appDomainAppVPath)
。HttpRuntime._theRuntime._appDomainAppVPath
is set in HttpRuntime.Init()
. HttpRuntime._theRuntime._appDomainAppVPath
在HttpRuntime.Init()
中设置。HttpRuntime.Init()
sets _appDomainAppVPath
from HttpRuntime.GetAppDomainString(".appVPath"))
. HttpRuntime.Init()
从HttpRuntime.GetAppDomainString(".appVPath"))
设置_appDomainAppVPath
。
AppDomain
. AppDomain
关联的小型可序列化标量值。System.Web
) creates a new AppDomain
for each Application Scope in IIS.System.Web
)为 IIS 中的每个应用程序 Scope创建一个新的AppDomain
。String
value for ".appVPath"
comes from...".appVPath"
的String
值从何而来……System.Web.Hosting.ApplicationManager::PopulateDomainBindings
sets dict.Add(".appVPath", appVPath.VirtualPathString)
System.Web.Hosting.ApplicationManager::PopulateDomainBindings
设置dict.Add(".appVPath", appVPath.VirtualPathString)
Host
header bindings in IIS. Yay for overloaded terminology.Host
header 绑定完全无关。是的,重载术语。PopulateDomainBindings
is called by System.Web.Hosting.ApplicationManager::CreateAppDomainWithHostingEnvironment
. PopulateDomainBindings
由System.Web.Hosting.ApplicationManager::CreateAppDomainWithHostingEnvironment
调用。
virtualPath: VirtualPath.Create(appHost.GetVirtualPath())
.virtualPath: VirtualPath.Create(appHost.GetVirtualPath())
。appHost.GetVirtualPath()
is IApplicationHost.GetVirtualPath()
. appHost.GetVirtualPath()
是IApplicationHost.GetVirtualPath()
。
System.Web.Hosting.ISAPIApplicationHost
and System.Web.Hosting.SimpleApplicationHost
.System.Web.Hosting.ISAPIApplicationHost
和System.Web.Hosting.SimpleApplicationHost
。 We're interested in ISAPIApplicationHost
.ISAPIApplicationHost
感兴趣。ISAPIApplicationHost
gets its virtualPath from the runtime argument to String appId
and String appPath
in the IAppManagerAppDomainFactory.Create
method. ISAPIApplicationHost
从运行时参数到IAppManagerAppDomainFactory.Create
方法中的String appId
和String appPath
appPath 获取其 virtualPath。
IAppManagerAppDomainFactory
is a COM interface used directly by IIS.IAppManagerAppDomainFactory
是IIS直接使用的一个COM接口。
IAppManagerAppDomainFactory
but came up empty-handed.IAppManagerAppDomainFactory
定义的痕迹,但一无所获。
webengine4.dll
which is a native DLL and I don't have the time to bust-out Ghidra right now...webengine4.dll
处理,它是原生的 DLL,我现在没有时间淘汰 Ghidra ...HttpExtensionProc
(and its LPEXTENSION_CONTROL_BLOCK
parameter ) do not contain the IIS Application Scope AppId
or Virtual Path, which surprised me - but most importantly: this suggests the value must likely come from the GetServerVariable
or ServerSupportFunction
callbacks....HttpExtensionProc
(及其LPEXTENSION_CONTROL_BLOCK
参数)不包含 IIS 应用程序 Scope AppId
或虚拟路径,这让我感到惊讶 - 但最重要的是:这表明该值可能来自GetServerVariable
或ServerSupportFunction
回调。 ...IWpfApplicationInfoUtil::GetApplicationPropertiesFromAppId
method (here "WPF" means " Worker Process Framework ", and is entirely unrelated to the other UI-related WPF).IWpfApplicationInfoUtil::GetApplicationPropertiesFromAppId
方法(这里的“WPF”表示“ Worker Process Framework ”,与其他与 UI 相关的 WPF 完全无关)。
IMetadataInfo.GetMetaPath()
method (which returns a string of the form "LM/WEBROOT/AppHost/{SiteId}"
).IMetadataInfo.GetMetaPath()
方法公开的相同数据(该方法返回"LM/WEBROOT/AppHost/{SiteId}"
形式的字符串)。IWpfApplicationInfoUtil
interface reference?IWpfApplicationInfoUtil
接口引用呢?
IWorkerProcessFramework->GetWpfInterface(WPF_APPLICATION_INFO_UTIL_ID)->GetApplicationPropertiesFromAppId
IWorkerProcessFramework
?IWorkerProcessFramework
呢?
w3wp.exe
worker DLL . w3wp.exe
worker DLL 。AspNetCoreModuleV2
does...AspNetCoreModuleV2
做了什么......AspNetCoreModuleV2
uses IIS's IHttpApplication
, derp . AspNetCoreModuleV2
使用 IIS 的IHttpApplication
, derp 。
IHttpApplication
exposes GetAppConfigPath()
which also returns a string of the form /LM/W3SVC/1/ROOT/{Site Name}
. IHttpApplication
公开GetAppConfigPath()
,它还返回/LM/W3SVC/1/ROOT/{Site Name}
形式的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.