Setting IHostingEnvironment.IsDevelopment as True in an ASP.NET 5 Application

Wondering why IHostingEnvironment.IsDevelopment returns false even when you are on you development machine? I did indeed wonder and here is why :)
13 September 2015
2 minutes read

Related Posts

I am now in Frankfurt, sipping my coffee in a Starbucks shop and enjoying its rubbish internet connection (as usual). I have just thrown away 10 minutes from my life by trying to figure out why my ASP.NET 5 application wasn't showing the error page. So, I wanted to write this quick and dirty blog post for people who will potentially have the same problem :)

Here is the piece of code I have inside the Configure method of my Startup class:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    // ...

    // Add the following to the request pipeline only in development environment.
    if (env.IsDevelopment())
    {
        app.UseErrorPage();
    }
    else
    {
        // Add Error handling middleware which catches all application specific errors and
        // send the request to the following path or controller action.
        app.UseErrorHandler("/Home/Error");
    }

    // ...

So, I should see the beautiful and detailed error page when I am in development. However, all I get is nothing but an empty response body when I run the application:

image

image

With a little bit of digging, I remembered that your environment is being determined through an environment variable which is ASPNET_ENV. Setting this to Development will return true from IHostingEnvironment.IsDevelopment. Also, the IHostingEnvironment.EnvironmentName will get you the value of this environment variable. You can set this environment variable per process, per user or per machine. Whatever floats your boat. I have set this for process on windows with the below script and I was able to get the lovely error page:

set ASPNET_ENV=Development

image

image

When you are on Visual Studio 2015, you can handle this better by adding a launchSettings.json file as here. VS will pick this up and set the environment variable for IIS Express process.