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.
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
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.
The following sample app shows how to do it: https://github.com/aspnet/samples/tree/master/samples/aspnetcore/mvc/runtimecompilation
@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 :(
Most helpful comment
The following sample app shows how to do it: https://github.com/aspnet/samples/tree/master/samples/aspnetcore/mvc/runtimecompilation