Reputation: 1717
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:
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). However, it doesn't exist in .NET 6.
I tried:
HttpContext.Request.PathBase
, but it returns the path exactly as in the requesting URL, not as in the IISIServerAddressesFeature
and IWebHostEnvironment
, but none of them contains the name from IIS with correct casingIServerAddressesFeature
, but also didn't find anything relevant hereIServerVariablesFeature 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
)Does anyone know how to get this site's name as configured in IIS (with proper casing)?
Upvotes: 2
Views: 2665
Reputation: 155145
APPL_MD_PATH
server-variable.
"
APPL_MD_PATH
- Retrieves the metabase path of the application."
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.
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.
ApplicationRoot
?Time to bust-out ILSpy...
HttpRequest.ApplicationPath
is HttpRuntime.AppDomainAppVirtualPath
.HttpRuntime.AppDomainAppVirtualPath
is VirtualPath.GetVirtualPathStringNoTrailingSlash(HttpRuntime._theRuntime._appDomainAppVPath)
.HttpRuntime._theRuntime._appDomainAppVPath
is set in HttpRuntime.Init()
.HttpRuntime.Init()
sets _appDomainAppVPath
from HttpRuntime.GetAppDomainString(".appVPath"))
.
AppDomain
.System.Web
) creates a new AppDomain
for each Application Scope in IIS.String
value for ".appVPath"
comes from...System.Web.Hosting.ApplicationManager::PopulateDomainBindings
sets dict.Add(".appVPath", appVPath.VirtualPathString)
Host
header bindings in IIS. Yay for overloaded terminology.PopulateDomainBindings
is called by System.Web.Hosting.ApplicationManager::CreateAppDomainWithHostingEnvironment
.
virtualPath: VirtualPath.Create(appHost.GetVirtualPath())
.appHost.GetVirtualPath()
is IApplicationHost.GetVirtualPath()
.
System.Web.Hosting.ISAPIApplicationHost
and System.Web.Hosting.SimpleApplicationHost
. We're interested in ISAPIApplicationHost
.ISAPIApplicationHost
gets its virtualPath from the runtime argument to String appId
and String appPath
in the IAppManagerAppDomainFactory.Create
method.
IAppManagerAppDomainFactory
is a COM interface used directly by IIS.
IAppManagerAppDomainFactory
but came up empty-handed.
webengine4.dll
which is a native DLL and I don't have the time to bust-out Ghidra right now...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....IWpfApplicationInfoUtil::GetApplicationPropertiesFromAppId
method (here "WPF" means "Worker Process Framework", and is entirely unrelated to the other UI-related WPF).
IMetadataInfo.GetMetaPath()
method (which returns a string of the form "LM/WEBROOT/AppHost/{SiteId}"
).IWpfApplicationInfoUtil
interface reference?
IWorkerProcessFramework->GetWpfInterface(WPF_APPLICATION_INFO_UTIL_ID)->GetApplicationPropertiesFromAppId
IWorkerProcessFramework
?
w3wp.exe
worker DLL.AspNetCoreModuleV2
does...AspNetCoreModuleV2
uses IIS's IHttpApplication
, derp.
IHttpApplication
exposes GetAppConfigPath()
which also returns a string of the form /LM/W3SVC/1/ROOT/{Site Name}
.Upvotes: 5