Viewing Server Variables in Asp.Net Core

In Asp.Net Core, you can view a list of data known as Server Variables. These are available when hosting inside IIS, and they give information about the request call and security.

You can view any one of these by adding some middleware to your Asp.Net Core project:

    app.Use(async (context, next) =>
        var url = context.Features.Get<IServerVariablesFeature>()["URL"];

        await next.Invoke();

You’ll need to be hosting inside IIS, and the code should go in the routing zone.

There is also a helper method for this, meaning you can do this instead:

    app.Use(async (context, next) =>
        string a = context.GetServerVariable("URL");

        await next.Invoke();

I then thought, why not just enumerate all the variables. You can see the list here.

I created a list of these:

    public static class ServerVariables
        public static string[] Variables = new[]

And then, in the middleware, just looped through them:

            app.Use(async (context, next) =>
                await context.Response.WriteAsync("<div>");
                foreach (var variable in ServerVariables.Variables)
                    string result = context.GetServerVariable(variable);
                    await context.Response.WriteAsync($"<p>{variable}:    <b>{result}</b></p><br />");

                await context.Response.WriteAsync("</div>");
                await next.Invoke();

Remember that if you run this hosted in Kestrel, you’ll get nothing back.


This site uses Akismet to reduce spam. Learn how your comment data is processed.