tsdaemon tsdaemon - 2 months ago 32
ASP.NET (C#) Question

How to set IHostingEnvironment.EnvironmentName in vNext application?

Default vNext web project contain such lines in

Startup.cs
:

if (string.Equals(env.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase))
{
app.UseBrowserLink();
app.UseErrorPage(ErrorPageOptions.ShowAll);
}
else
{
app.UseErrorHandler("/Home/Error");
}


As I understand, the EnvironmentName is a new way to handle Dev/Production environment. But it doesn't changes on Release build configuration. So what is the way to set a different
EnvironmentName
?

I can imagine that it should be set in "Commands" as a parameter for server.

Answer

I can imagine that it should be set in "Commands" as a parameter for server.

That is true. In your project.json, add --ASPNET_ENV production as a parameter for the server.

project.json

"commands": {
  "web": "Microsoft.AspNet.Hosting --ASPNET_ENV production --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5001"
}

Now when you run dnx . web from the command line, ASPNET_ENV will be production.

Azure

We cannot also set in from the Azure WEB APP > CONFIGURE > app settings area.

Set Environment Name

Comments