Aspnetcore.docs: Under "Conditionally enable runtime compilation" Env is coming as NULL

Created on 6 Jan 2020  ·  8Comments  ·  Source: dotnet/AspNetCore.Docs

Documentation does not say where to assign Env.

Just following the documentation as as is under "Conditionally enable runtime compilation" throws a null reference exception when trying to use Env.IsDevelopment().

I had to assign Env as below

public Startup(IConfiguration configuration, IWebHostEnvironment env) {
    Configuration = configuration;
    Env = env;
}

Then I am able to use Env.IsDevelopment() without throwing a null reference exception.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Source - Docs.ms doc-bug

Most helpful comment

All 8 comments

Create a new project - which shows the correct usage.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }

You're right, I'll get his updated.

@scottaddie Thank you. That is exactly what I did. I wanted to make sure I was doing it the correct way 👍

I hope the Docs get updated for other people having the same problem

@esausilva I'll get it updated this week. Thanks for reporting the problem.

@scottaddie - in the sample is it perhaps better to remove the env parameter from the Configure method? Else it feels like the class level property might be a different IWebHostEnvironment.

Unsure if you're a maintainer, but have created a PR showing my thoughts.

@nzcoward Let's see what @pranavkm thinks of your proposal.

Sure, done.

Not sure why 3 months on, the docs are still not correct :(

Was this page helpful?
0 / 5 - 0 ratings