简体   繁体   English

ASP.NET Core 6.0+: 如何获取当前(区分大小写)IIS 网站名称?

[英]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 中的路径
  • injecting IServerAddressesFeature and IWebHostEnvironment , but none of them contains the name from IIS with correct casing注入IServerAddressesFeatureIWebHostEnvironment ,但它们都不包含来自 IIS 的名称以及正确的大小写
  • IServerAddressesFeature , but also didn't find anything relevant here IServerAddressesFeature ,但也没有在此处找到任何相关内容
  • getting server variables: 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 中配置的该站点的名称(使用正确的大小写)?

TL;DR:长话短说:

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"将不可用,因此请确保您也在处理这种情况。


Original research: What happened to ApplicationRoot ?原创研究: ApplicationRoot发生了什么?

Time to bust-out ILSpy...是时候淘汰ILSpy 了……

  1. HttpRequest.ApplicationPath is HttpRuntime.AppDomainAppVirtualPath . HttpRequest.ApplicationPathHttpRuntime.AppDomainAppVirtualPath
  2. HttpRuntime.AppDomainAppVirtualPath is VirtualPath.GetVirtualPathStringNoTrailingSlash(HttpRuntime._theRuntime._appDomainAppVPath) . HttpRuntime.AppDomainAppVirtualPathVirtualPath.GetVirtualPathStringNoTrailingSlash(HttpRuntime._theRuntime._appDomainAppVPath)
  3. HttpRuntime._theRuntime._appDomainAppVPath is set in HttpRuntime.Init() . HttpRuntime._theRuntime._appDomainAppVPathHttpRuntime.Init()中设置。
  4. HttpRuntime.Init() sets _appDomainAppVPath from HttpRuntime.GetAppDomainString(".appVPath")) . HttpRuntime.Init()HttpRuntime.GetAppDomainString(".appVPath"))设置_appDomainAppVPath
  5. System.Web.Hosting.ApplicationManager::PopulateDomainBindings sets dict.Add(".appVPath", appVPath.VirtualPathString) System.Web.Hosting.ApplicationManager::PopulateDomainBindings设置dict.Add(".appVPath", appVPath.VirtualPathString)
    • "domain bindings" in this context refers to AppDomain bindings : nothing at all to do with DNS domain-names or Host header bindings in IIS. Yay for overloaded terminology.此上下文中的“域绑定”指的是AppDomain 绑定:与 DNS 域名或 IIS 中的Host header 绑定完全无关。是的,重载术语。
  6. PopulateDomainBindings is called by System.Web.Hosting.ApplicationManager::CreateAppDomainWithHostingEnvironment . PopulateDomainBindingsSystem.Web.Hosting.ApplicationManager::CreateAppDomainWithHostingEnvironment调用。
    • And it gets virtualPath: VirtualPath.Create(appHost.GetVirtualPath()) .它得到virtualPath: VirtualPath.Create(appHost.GetVirtualPath())
  7. appHost.GetVirtualPath() is IApplicationHost.GetVirtualPath() . appHost.GetVirtualPath()IApplicationHost.GetVirtualPath()
    • There are 2 in-box implementations: System.Web.Hosting.ISAPIApplicationHost and System.Web.Hosting.SimpleApplicationHost .有 2 个内置实现: System.Web.Hosting.ISAPIApplicationHostSystem.Web.Hosting.SimpleApplicationHost We're interested in ISAPIApplicationHost .我们对ISAPIApplicationHost感兴趣。
  8. ISAPIApplicationHost gets its virtualPath from the runtime argument to String appId and String appPath in the IAppManagerAppDomainFactory.Create method. ISAPIApplicationHost从运行时参数到IAppManagerAppDomainFactory.Create方法中的String appIdString appPath appPath 获取其 virtualPath。
    • And IAppManagerAppDomainFactory is a COM interface used directly by IIS.IAppManagerAppDomainFactory是IIS直接使用的一个COM接口。
      • The plot thickens... plot加厚...
  9. At this point I got lost trawling through the legacy IIS 6 ISAPI documentation , looking for traces of the original COM definition of IAppManagerAppDomainFactory but came up empty-handed.在这一点上,我在遗留的 IIS 6 ISAPI 文档中迷失了方向,寻找 IAppManagerAppDomainFactory 的原始IAppManagerAppDomainFactory定义的痕迹,但一无所获。
    • It's probably handled by webengine4.dll which is a native DLL and I don't have the time to bust-out Ghidra right now...它可能由webengine4.dll处理,它是原生的 DLL,我现在没有时间淘汰 Ghidra ...
    • I did notice that the ISAPI request entrypoint method 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....我确实注意到 ISAPI 请求入口点方法HttpExtensionProc (及其LPEXTENSION_CONTROL_BLOCK参数包含 IIS 应用程序 Scope AppId或虚拟路径,这让我感到惊讶 - 但最重要的是:这表明该值可能来自GetServerVariableServerSupportFunction回调。 ...
    • 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...).然而,无论如何这可能是浪费时间 IIS 6不是IIS7+ 并且 IIS7+ 的接口不再称为“ISAPI”,而是仅称为“IIS Native-Code API”(这是我在尽管...)。
  10. So, starting with the "IIS Native-Code API" documentation I quickly found the IWpfApplicationInfoUtil::GetApplicationPropertiesFromAppId method (here "WPF" means " Worker Process Framework ", and is entirely unrelated to the other UI-related WPF).因此,从“IIS Native-Code API”文档开始, 我很快找到了IWpfApplicationInfoUtil::GetApplicationPropertiesFromAppId方法(这里的“WPF”表示“ Worker Process Framework ”,与其他与 UI 相关的 WPF 完全无关)。
  11. Actually AspNetCoreModuleV2 uses IIS's IHttpApplication , derp . 实际上AspNetCoreModuleV2使用 IIS 的IHttpApplicationderp
  12. At this point I gave up as it's now 6:20am, but that was a fun dive!此时我放弃了,因为现在是早上 6 点 20 分,但那是一次有趣的潜水!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net网站项目如何区分大小写? - How to compile asp.net website project as case sensitive? 从 ASP.NET 网站获取 IIS 站点名称 - Get IIS site name from for an ASP.NET website 如何使用 ASP.NET Core RC2 MVC6 和 IIS7 获取当前的 Windows 用户 - How to get the current Windows user with ASP.NET Core RC2 MVC6 and IIS7 如何在 C#/.NET 中找到文件的真实(区分大小写)名称? - How to find the true (case-sensitive) name of a file in C#/.NET? 如何在asp.net核心中调用c#方法的ajax上获取当前主机名和路径名 - How to get current host name and path name on ajax calling c# method in asp.net core 如何从ASP.Net网站获取当前图像名称? - How do you get the current image name from an ASP.Net website? 如何使用 IIS Express 在 ASP.NET Core 中获取控制台输出 - How to get a Console output in ASP.NET Core with IIS Express 为什么MySql表名称区分大小写? - Why is a MySql Table name case-sensitive? 如何在ASP.NET Core中获取Active Directory当前用户显示名称? - How to get Active Directory current user Display Name in ASP.NET Core? 如何使用 ASP.NET Core 获取当前路由名称? - How can I get the current route name with ASP.NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM