Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Asp.Net(Core) application is hosted in IIS

Tags:

asp.net

iis

How can I check inside the application if it is being hosted in IIS?

like image 789
Skorunka František Avatar asked Feb 16 '17 11:02

Skorunka František


People also ask

How do I know if ASP.NET is registered with IIS?

To check that ASP.NET is installed and registered with the correct version, enter the command aspnet_regiis.exe -lv at the command prompt.

How do I know if NET Core hosting is installed?

If you need to find out if the . NET Core is installed in Windows and which version of the SDK is installed , you can use the dotnet –info command from the Windows command prompt.


2 Answers

Check if the environment variable APP_POOL_ID is set.

public static bool InsideIIS() =>
    System.Environment.GetEnvironmentVariable("APP_POOL_ID") is string;

All of environment variables that iis sets on a child process

like image 167
Branimir Ričko Avatar answered Nov 06 '22 10:11

Branimir Ričko


I've tried the answer by Branimir Ričko but found that it's not correct: this environment variable is also set when running under IIS express.

So here is my modified version:

static bool IsRunningInsideIIS() =>
    System.Environment.GetEnvironmentVariable("ASPNETCORE_HOSTINGSTARTUPASSEMBLIES") is string startupAssemblies &&  
 startupAssemblies.Contains(typeof(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults).Namespace);
like image 42
Remigijus Pankevičius Avatar answered Nov 06 '22 09:11

Remigijus Pankevičius