Lỗi it is not supported in mobile environment năm 2024

SAP Fiori for SAP S/4HANA all versions ; SAP Fiori front-end server all versions ; SAP NetWeaver Application Server for ABAP innovation package all versions

Keywords

incompatible, analytics, kpi, smart business, sb, frontend, front-end, front end, 4.0, Manage Recipes, PLM Recipe, Fiori launchpad, outdated, purchasing, F2343, , KBA , quality engineer overview , CA-FLP-ABA , SAP Fiori Launchpad ABAP Services , FIN-FSCM-CLM-BAM , Bank Account Management , Problem

About this page

This is a preview of a SAP Knowledge Base Article. Click more to access the full version on SAP for Me (Login required).

Hi everyone. I am trying to set up a development environment for Moodle Mobile 2. I followed the exact steps as mentioned in this page: https://docs.moodle.org/dev/Setting_up_your_development_environment_for_Moodle_Mobile_2

When I run npm run setup, I am getting many errors. Please find the attachment below.

Following are the steps I did:

1. Installed Git

2. Installed chromium

3. Installed nvm setup for windows (https://github.com/coreybutler/nvm-windows/releases)

4. npm cache clean --force

5. npm install -g [email protected] ionic

npm WARN deprecated [email protected]: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header). This notice will go away with v5.0.2+ once it is released.

C:\Program Files\nodejs\cordova -> C:\Program Files\nodejs\node_modules\cordova\bin\cordova

C:\Program Files\nodejs\ionic -> C:\Program Files\nodejs\node_modules\ionic\bin\ionic

+ [email protected]

+ [email protected]

added 834 packages from 577 contributors in 93.775s

6. npm install -g gulp

npm WARN deprecated [email protected]: One of your dependencies needs to upgrade to fsevents v2: 1) Proper nodejs v10+ support 2) No more fetching binaries from AWS, smaller package size

C:\Program Files\nodejs\gulp -> C:\Program Files\nodejs\node_modules\gulp\bin\gulp.js

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\gulp\node_modules\fsevents):

npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Use multiple environments in ASP.NET Core

  • Article
  • 04/26/2024

In this article

By Rick Anderson and Kirk Larkin

ASP.NET Core configures app behavior based on the runtime environment using an environment variable.

Environments

To determine the runtime environment, ASP.NET Core reads from the following environment variables:

  1. dotnet run --environment Production

    0 when the WebApplication.CreateBuilder method is called. The default ASP.NET Core web app templates call

    dotnet run --environment Production

    1. The

    dotnet run --environment Production

    0 value overrides

    dotnet run --environment Production

    3.

To determine the runtime environment, ASP.NET Core reads from the following environment variables:

  1. dotnet run --environment Production

    0 when the WebApplication.CreateBuilder method is called. The default ASP.NET Core web app templates call

    dotnet run --environment Production

    1. The

    dotnet run --environment Production

    3 value overrides

    dotnet run --environment Production

    0 when

    dotnet run --environment Production

    8 is used. For other hosts, such as

    dotnet run --environment Production

    9 and

    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7262  
    
    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5005  
    
    info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.  
    
    info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production  
    
    info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample  
    

    0,

    dotnet run --environment Production

    0 has higher precedence.

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7262
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5005
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample

2 can be set to any value, but the following values are provided by the framework:

  • : The file sets

    dotnet run --environment Production

    0 to

    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7262  
    
    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5005  
    
    info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.  
    
    info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production  
    
    info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample  
    
    4 on the local machine.
  • : The default if

    dotnet run --environment Production

    3 and

    dotnet run --environment Production

    0 have not been set.

The following code:

  • Is similar to the code generated by the ASP.NET Core templates.
  • Enables the when

    dotnet run --environment Production

    0 is set to

    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7262  
    
    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5005  
    
    info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.  
    
    info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production  
    
    info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample  
    
    4. This is done automatically by the WebApplication.CreateBuilder method.
  • Calls UseExceptionHandler when the value of

    dotnet run --environment Production

    0 is anything other than

    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7262  
    
    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5005  
    
    info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.  
    
    info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production  
    
    info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample  
    
    4.
  • Provides an IWebHostEnvironment instance in the property of

    { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    1.

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    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.MapRazorPages();
app.Run();

The Environment Tag Helper uses the value of to include or exclude markup in the element:


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

The About page from the sample code includes the preceding markup and displays the value of

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

2.

On Windows and macOS, environment variables and values aren't case-sensitive. Linux environment variables and values are case-sensitive by default.

Create EnvironmentsSample

The sample code used in this article is based on a Razor Pages project named EnvironmentsSample.

The following .NET CLI commands create and run a web app named EnvironmentsSample:

dotnet new webapp -o EnvironmentsSample
cd EnvironmentsSample
dotnet run --verbosity normal

When the app runs, it displays output similar to the following:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7152
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5105
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample

Set environment on the command line

Use the

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

3 flag to set the environment. For example:

dotnet run --environment Production

The preceding command sets the environment to

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

4 and displays output similar to the following in the command window:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7262
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5005
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample

Development and launchSettings.json

The development environment can enable features that shouldn't be exposed in production. For example, the ASP.NET Core project templates enable the in the development environment. Because of the performance cost, scope validation and dependency validation only happens in development.

The environment for local machine development can be set in the Properties\launchSettings.json file of the project. Environment values set in

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 override values set in the system environment.

The

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 file:

  • Is only used on the local development machine.
  • Is not deployed.
  • Contains profile settings.

The following JSON shows the

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 file for an ASP.NET Core web project named EnvironmentsSample created with Visual Studio or

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

8:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

The preceding JSON contains two profiles:

  • { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    9: The profile name is the project name. As the first profile listed, this profile is used by default. The

    { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    0 key has the value

    { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    1, therefore, the Kestrel web server is launched.
  • { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    2: The

    { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    0 key has the value

    { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    4, therefore, IISExpress is the web server.

You can set the launch profile to the project or any other profile included in

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5. For example, in the image below, selecting the project name launches the Kestrel web server.

Lỗi it is not supported in mobile environment năm 2024

The value of

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "EnvironmentsSample-Staging": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"
      }
    },
    "EnvironmentsSample-Production": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

6 can specify the web server to launch.

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "EnvironmentsSample-Staging": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"
      }
    },
    "EnvironmentsSample-Production": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

6 can be any one of the following:

  • { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    8 : Launches IIS Express.
  • { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    9 : No web server launched. IIS is expected to be available.
  • dotnet run --launch-profile "EnvironmentsSample"

    0 : Launches Kestrel.

The Visual Studio 2022 project properties Debug / General tab provides an Open debug launch profiles UI link. This link opens a Launch Profiles dialog that lets you edit the environment variable settings in the

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 file. You can also open the Launch Profiles dialog from the Debug menu by selecting Debug Properties. Changes made to project profiles may not take effect until the web server is restarted. Kestrel must be restarted before it can detect changes made to its environment.

Lỗi it is not supported in mobile environment năm 2024

The following

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 file contains multiple profiles:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "EnvironmentsSample-Staging": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"
      }
    },
    "EnvironmentsSample-Production": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Profiles can be selected:

  • From the Visual Studio UI.
  • Using the

    dotnet run --launch-profile "EnvironmentsSample"

    3 CLI command with the

    dotnet run --launch-profile "EnvironmentsSample"

    4 option set to the profile's name. This approach only supports Kestrel profiles.

    dotnet run --launch-profile "EnvironmentsSample"

Warning

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 shouldn't store secrets. The Secret Manager tool can be used to store secrets for local development.

When using Visual Studio Code, environment variables can be set in the

dotnet run --launch-profile "EnvironmentsSample"

6 file. The following example sets several :

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            // Configuration ommitted for brevity.
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development",
                "ASPNETCORE_URLS": "https://localhost:5001",
                "ASPNETCORE_DETAILEDERRORS": "1",
                "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"
            },
            // Configuration ommitted for brevity.

The

dotnet run --launch-profile "EnvironmentsSample"

6 file is used only by Visual Studio Code.

Production

The production environment should be configured to maximize security, performance, and application robustness. Some common settings that differ from development include:

  • Caching.
  • Client-side resources are bundled, minified, and potentially served from a CDN.
  • Diagnostic error pages disabled.
  • Friendly error pages enabled.
  • Production logging and monitoring enabled. For example, using Application Insights.

Set the environment by setting an environment variable

It's often useful to set a specific environment for testing with an environment variable or platform setting. If the environment isn't set, it defaults to

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

4, which disables most debugging features. The method for setting the environment depends on the operating system.

When the host is built, the last environment setting read by the app determines the app's environment. The app's environment can't be changed while the app is running.

The About page from the sample code displays the value of

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

2.

Azure App Service

is the default value if

dotnet run --environment Production

3 and

dotnet run --environment Production

0 have not been set. Apps deployed to Azure are

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

4 by default.

To set the environment in an Azure App Service app by using the portal:

  1. Select the app from the App Services page.
  2. In the Settings group, select Configuration.
  3. In the Application settings tab, select New application setting.
  4. In the Add/Edit application setting window, provide

    dotnet run --environment Production

    0 for the Name. For Value, provide the environment (for example,

    {
    "version": "0.2.0",  
    "configurations": [  
        {  
            "name": ".NET Core Launch (web)",  
            "type": "coreclr",  
            // Configuration ommitted for brevity.  
            "env": {  
                "ASPNETCORE_ENVIRONMENT": "Development",  
                "ASPNETCORE_URLS": "https://localhost:5001",  
                "ASPNETCORE_DETAILEDERRORS": "1",  
                "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
            },  
            // Configuration ommitted for brevity.  
    
    4).
  5. Select the Deployment slot setting checkbox if you wish the environment setting to remain with the current slot when deployment slots are swapped. For more information, see Set up staging environments in Azure App Service in the Azure documentation.
  6. Select OK to close the Add/Edit application setting dialog.
  7. Select Save at the top of the Configuration page.

Azure App Service automatically restarts the app after an app setting is added, changed, or deleted in the Azure portal.

Windows - Set environment variable for a process

Environment values in

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 override values set in the system environment.

To set the

dotnet run --environment Production

0 for the current session when the app is started using dotnet run, use the following commands at a command prompt or in PowerShell:


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

0


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

1

Windows - Set environment variable globally

The preceding commands set

dotnet run --environment Production

0 only for processes launched from that command window.

To set the value globally in Windows, use either of the following approaches:

  • Open the Control Panel > System > Advanced system settings and add or edit the

    dotnet run --environment Production

    0 value:
    Lỗi it is not supported in mobile environment năm 2024
    Lỗi it is not supported in mobile environment năm 2024
  • Open an administrative command prompt and use the {
    "version": "0.2.0",  
    "configurations": [  
        {  
            "name": ".NET Core Launch (web)",  
            "type": "coreclr",  
            // Configuration ommitted for brevity.  
            "env": {  
                "ASPNETCORE_ENVIRONMENT": "Development",  
                "ASPNETCORE_URLS": "https://localhost:5001",  
                "ASPNETCORE_DETAILEDERRORS": "1",  
                "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
            },  
            // Configuration ommitted for brevity.  
    

    9 command or open an administrative PowerShell command prompt and use

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    00:

    •    
      Environment is Development
         
      Environment is NOT Development
         
      Environment is: Staging, Development or Staging_2

      2 The

         
      Environment is Development
         
      Environment is NOT Development
         
      Environment is: Staging, Development or Staging_2

      01 switch sets the environment variable at the system level. If the

         
      Environment is Development
         
      Environment is NOT Development
         
      Environment is: Staging, Development or Staging_2

      01 switch isn't used, the environment variable is set for the user account.
    •    
      Environment is Development
         
      Environment is NOT Development
         
      Environment is: Staging, Development or Staging_2

      3 The

         
      Environment is Development
         
      Environment is NOT Development
         
      Environment is: Staging, Development or Staging_2

      03 option sets the environment variable at the system level. If the option value is changed to

         
      Environment is Development
         
      Environment is NOT Development
         
      Environment is: Staging, Development or Staging_2

      04, the environment variable is set for the user account.

When the

dotnet run --environment Production

0 environment variable is set globally, it takes effect for

dotnet run --launch-profile "EnvironmentsSample"

3 in any command window opened after the value is set. Environment values in

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 override values set in the system environment.

Windows - Use web.config

To set the

dotnet run --environment Production

0 environment variable with


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

09, see the Set environment variables section of .

Windows - IIS deployments

Include the


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

10 property in the publish profile (.pubxml) or project file. This approach sets the environment in web.config when the project is published:


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

4

To set the

dotnet run --environment Production

0 environment variable for an app running in an isolated Application Pool (supported on IIS 10.0 or later), see the AppCmd.exe command section of . When the

dotnet run --environment Production

0 environment variable is set for an app pool, its value overrides a setting at the system level.

When hosting an app in IIS and adding or changing the

dotnet run --environment Production

0 environment variable, use one of the following approaches to have the new value picked up by apps:

  • Execute

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    14 followed by

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    15 from a command prompt.
  • Restart the server.

macOS

Setting the current environment for macOS can be performed in-line when running the app:


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

5

Alternatively, set the environment with


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

16 prior to running the app:


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

6

Machine-level environment variables are set in the .bashrc or .bash_profile file. Edit the file using any text editor. Add the following statement:


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

6

Linux

For Linux distributions, use the


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

16 command at a command prompt for session-based variable settings and the bash_profile file for machine-level environment settings.

Set the environment in code

To set the environment in code, use when creating WebApplicationBuilder, as shown in the following example:


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

8

Configuration by environment

To load configuration by environment, see .

Configure services and middleware by environment

Use or to conditionally add services or middleware depending on the current environment. The project template includes an example of code that adds middleware only when the current environment isn't Development:

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    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.MapRazorPages();
app.Run();

The highlighted code checks the current environment while building the request pipeline. To check the current environment while configuring services, use


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

18 instead of


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

19.

Additional resources

  • View or download sample code ()
  • App startup in ASP.NET Core
  • Configuration in ASP.NET Core
  • ASP.NET Core Blazor environments

By Rick Anderson and Kirk Larkin

ASP.NET Core configures app behavior based on the runtime environment using an environment variable.

Environments

To determine the runtime environment, ASP.NET Core reads from the following environment variables:

  1. dotnet run --environment Production

    0 when ConfigureWebHostDefaults is called. The default ASP.NET Core web app templates call

    dotnet run --environment Production

    9. The

    dotnet run --environment Production

    0 value overrides

    dotnet run --environment Production

    3.

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7262
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5005
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample

2 can be set to any value, but the following values are provided by the framework:

  • : The file sets

    dotnet run --environment Production

    0 to

    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7262  
    
    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5005  
    
    info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.  
    
    info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production  
    
    info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample  
    
    4 on the local machine.
  • : The default if

    dotnet run --environment Production

    3 and

    dotnet run --environment Production

    0 have not been set.

The following code:

  • Calls UseDeveloperExceptionPage when

    dotnet run --environment Production

    0 is set to

    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7262  
    
    info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5005  
    
    info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.  
    
    info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production  
    
    info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample  
    
    4.
  • Calls UseExceptionHandler when the value of

    dotnet run --environment Production

    0 is set to

    {
    "version": "0.2.0",  
    "configurations": [  
        {  
            "name": ".NET Core Launch (web)",  
            "type": "coreclr",  
            // Configuration ommitted for brevity.  
            "env": {  
                "ASPNETCORE_ENVIRONMENT": "Development",  
                "ASPNETCORE_URLS": "https://localhost:5001",  
                "ASPNETCORE_DETAILEDERRORS": "1",  
                "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
            },  
            // Configuration ommitted for brevity.  
    

    4,

    { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    4, or

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    34.
  • Injects IWebHostEnvironment into

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    35. This approach is useful when the app only requires adjusting

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    35 for a few environments with minimal code differences per environment.
  • Is similar to the code generated by the ASP.NET Core templates.

dotnet new webapp -o EnvironmentsSample
cd EnvironmentsSample
dotnet run --verbosity normal

0

The Environment Tag Helper uses the value of to include or exclude markup in the element:

dotnet new webapp -o EnvironmentsSample
cd EnvironmentsSample
dotnet run --verbosity normal

1

The About page from the sample code includes the preceding markup and displays the value of

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

2.

On Windows and macOS, environment variables and values aren't case-sensitive. Linux environment variables and values are case-sensitive by default.

Create EnvironmentsSample

The sample code used in this document is based on a Razor Pages project named EnvironmentsSample.

The following code creates and runs a web app named EnvironmentsSample:

dotnet new webapp -o EnvironmentsSample
cd EnvironmentsSample
dotnet run --verbosity normal

When the app runs, it displays some of the following output:

dotnet new webapp -o EnvironmentsSample
cd EnvironmentsSample
dotnet run --verbosity normal

3

Development and launchSettings.json

The development environment can enable features that shouldn't be exposed in production. For example, the ASP.NET Core templates enable the in the development environment.

The environment for local machine development can be set in the Properties\launchSettings.json file of the project. Environment values set in

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 override values set in the system environment.

The

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 file:

  • Is only used on the local development machine.
  • Is not deployed.
  • contains profile settings.

The following JSON shows the

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 file for an ASP.NET Core web project named EnvironmentsSample created with Visual Studio or

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

8:

dotnet new webapp -o EnvironmentsSample
cd EnvironmentsSample
dotnet run --verbosity normal

4

The preceding markup contains two profiles:

  • { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    2: The default profile used when launching the app from Visual Studio. The

    { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    0 key has the value

    { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    4, therefore, IISExpress is the web server. You can set the launch profile to the project or any other profile included. For example, in the image below, selecting the project name launches the Kestrel web server.
    Lỗi it is not supported in mobile environment năm 2024
  • { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    9: The profile name is the project name. This profile is used by default when launching the app with

    dotnet run --launch-profile "EnvironmentsSample"

    3. The

    { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    0 key has the value

    { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    1, therefore, the Kestrel web server is launched.

The value of

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "EnvironmentsSample-Staging": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"
      }
    },
    "EnvironmentsSample-Production": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

6 can specify the web server to launch.

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "EnvironmentsSample-Staging": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"
      }
    },
    "EnvironmentsSample-Production": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

6 can be any one of the following:

  • { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    8 : Launches IIS Express.
  • { "iisSettings": {

    "windowsAuthentication": false,  
    "anonymousAuthentication": true,  
    "iisExpress": {  
      "applicationUrl": "http://localhost:59481",  
      "sslPort": 44308  
    }  
    
    }, "profiles": {
    "EnvironmentsSample": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    },  
    "EnvironmentsSample-Staging": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Staging",  
        "ASPNETCORE_DETAILEDERRORS": "1",  
        "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
      }  
    },  
    "EnvironmentsSample-Production": {  
      "commandName": "Project",  
      "dotnetRunMessages": true,  
      "launchBrowser": true,  
      "applicationUrl": "https://localhost:7152;http://localhost:5105",  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Production"  
      }  
    },  
    "IIS Express": {  
      "commandName": "IISExpress",  
      "launchBrowser": true,  
      "environmentVariables": {  
        "ASPNETCORE_ENVIRONMENT": "Development"  
      }  
    }  
    
    } }

    9 : No web server launched. IIS is expected to be available.
  • dotnet run --launch-profile "EnvironmentsSample"

    0 : Launches Kestrel.

The Visual Studio project properties Debug tab provides a GUI to edit the

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 file. Changes made to project profiles may not take effect until the web server is restarted. Kestrel must be restarted before it can detect changes made to its environment.

Lỗi it is not supported in mobile environment năm 2024

The following

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 file contains multiple profiles:

dotnet new webapp -o EnvironmentsSample
cd EnvironmentsSample
dotnet run --verbosity normal

5

Profiles can be selected:

  • From the Visual Studio UI.
  • Using the

    dotnet run --launch-profile "EnvironmentsSample"

    3 command in a command shell with the

    dotnet run --launch-profile "EnvironmentsSample"

    4 option set to the profile's name. This approach only supports Kestrel profiles.

    dotnet new webapp -o EnvironmentsSample cd EnvironmentsSample dotnet run --verbosity normal

    6

Warning

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 shouldn't store secrets. The Secret Manager tool can be used to store secrets for local development.

When using Visual Studio Code, environment variables can be set in the

dotnet run --launch-profile "EnvironmentsSample"

6 file. The following example sets several :

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            // Configuration ommitted for brevity.
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development",
                "ASPNETCORE_URLS": "https://localhost:5001",
                "ASPNETCORE_DETAILEDERRORS": "1",
                "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"
            },
            // Configuration ommitted for brevity.

The

dotnet run --launch-profile "EnvironmentsSample"

6 file is only used by Visual Studio Code.

Production

The production environment should be configured to maximize security, performance, and application robustness. Some common settings that differ from development include:

  • Caching.
  • Client-side resources are bundled, minified, and potentially served from a CDN.
  • Diagnostic error pages disabled.
  • Friendly error pages enabled.
  • Production logging and monitoring enabled. For example, using Application Insights.

Set the environment

It's often useful to set a specific environment for testing with an environment variable or platform setting. If the environment isn't set, it defaults to

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

4, which disables most debugging features. The method for setting the environment depends on the operating system.

When the host is built, the last environment setting read by the app determines the app's environment. The app's environment can't be changed while the app is running.

The About page from the sample code displays the value of

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

2.

Azure App Service

is the default value if

dotnet run --environment Production

3 and

dotnet run --environment Production

0 have not been set. Apps deployed to azure are

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

4 by default.

To set the environment in Azure App Service, perform the following steps:

  1. Select the app from the App Services blade.
  2. In the Settings group, select the Configuration blade.
  3. In the Application settings tab, select New application setting.
  4. In the Add/Edit application setting window, provide

    dotnet run --environment Production

    0 for the Name. For Value, provide the environment (for example,

    {
    "version": "0.2.0",  
    "configurations": [  
        {  
            "name": ".NET Core Launch (web)",  
            "type": "coreclr",  
            // Configuration ommitted for brevity.  
            "env": {  
                "ASPNETCORE_ENVIRONMENT": "Development",  
                "ASPNETCORE_URLS": "https://localhost:5001",  
                "ASPNETCORE_DETAILEDERRORS": "1",  
                "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
            },  
            // Configuration ommitted for brevity.  
    
    4).
  5. Select the Deployment slot setting checkbox if you wish the environment setting to remain with the current slot when deployment slots are swapped. For more information, see Set up staging environments in Azure App Service in the Azure documentation.
  6. Select OK to close the Add/Edit application setting window.
  7. Select Save at the top of the Configuration blade.

Azure App Service automatically restarts the app after an app setting is added, changed, or deleted in the Azure portal.

Windows

Environment values in

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 override values set in the system environment.

To set the

dotnet run --environment Production

0 for the current session when the app is started using dotnet run, the following commands are used:

Command prompt


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

0

PowerShell


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

1

The preceding command sets

dotnet run --environment Production

0 only for processes launched from that command window.

To set the value globally in Windows, use either of the following approaches:

  • Open the Control Panel > System > Advanced system settings and add or edit the

    dotnet run --environment Production

    0 value:
    Lỗi it is not supported in mobile environment năm 2024
    Lỗi it is not supported in mobile environment năm 2024
  • Open an administrative command prompt and use the {
    "version": "0.2.0",  
    "configurations": [  
        {  
            "name": ".NET Core Launch (web)",  
            "type": "coreclr",  
            // Configuration ommitted for brevity.  
            "env": {  
                "ASPNETCORE_ENVIRONMENT": "Development",  
                "ASPNETCORE_URLS": "https://localhost:5001",  
                "ASPNETCORE_DETAILEDERRORS": "1",  
                "ASPNETCORE_SHUTDOWNTIMEOUTSECONDS": "3"  
            },  
            // Configuration ommitted for brevity.  
    

    9 command or open an administrative PowerShell command prompt and use

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    00: Command prompt

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    2 The

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    01 switch indicates to set the environment variable at the system level. If the

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    01 switch isn't used, the environment variable is set for the user account. PowerShell

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    3 The

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    03 option value indicates to set the environment variable at the system level. If the option value is changed to

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    04, the environment variable is set for the user account.

When the

dotnet run --environment Production

0 environment variable is set globally, it takes effect for

dotnet run --launch-profile "EnvironmentsSample"

3 in any command window opened after the value is set. Environment values in

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59481",
      "sslPort": 44308
    }
  },
  "profiles": {
    "EnvironmentsSample": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7152;http://localhost:5105",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

5 override values set in the system environment.

web.config

To set the

dotnet run --environment Production

0 environment variable with


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

09, see the Set environment variables section of .

Project file or publish profile

For Windows IIS deployments: Include the


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

10 property in the publish profile (.pubxml) or project file. This approach sets the environment in web.config when the project is published:


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

4

Per IIS Application Pool

To set the

dotnet run --environment Production

0 environment variable for an app running in an isolated Application Pool (supported on IIS 10.0 or later), see the AppCmd.exe command section of the topic. When the

dotnet run --environment Production

0 environment variable is set for an app pool, its value overrides a setting at the system level.

When hosting an app in IIS and adding or changing the

dotnet run --environment Production

0 environment variable, use any one of the following approaches to have the new value picked up by apps:

  • Execute

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    14 followed by

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    15 from a command prompt.
  • Restart the server.

macOS

Setting the current environment for macOS can be performed in-line when running the app:


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

5

Alternatively, set the environment with


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

16 prior to running the app:


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

6

Machine-level environment variables are set in the .bashrc or .bash_profile file. Edit the file using any text editor. Add the following statement:


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

6

Linux

For Linux distributions, use the


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

16 command at a command prompt for session-based variable settings and bash_profile file for machine-level environment settings.

Set the environment in code

Call UseEnvironment when building the host. See .

Configuration by environment

To load configuration by environment, see .

Environment-based Startup class and methods

Inject IWebHostEnvironment into the Startup class

Inject IWebHostEnvironment into the


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

91 constructor. This approach is useful when the app requires configuring


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

91 for only a few environments with minimal code differences per environment.

In the following example:

  • The environment is held in the

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    93 field.
  • Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    93 is used in

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    95 and

    Environment is Development
    Environment is NOT Development
    Environment is: Staging, Development or Staging_2

    96 to apply startup configuration based on the app's environment.

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7152
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5105
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample

6

Startup class conventions

When an ASP.NET Core app starts, the Startup class bootstraps the app. The app can define multiple


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

91 classes for different environments. The appropriate


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

91 class is selected at runtime. The class whose name suffix matches the current environment is prioritized. If a matching


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

99 class isn't found, the


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

91 class is used. This approach is useful when the app requires configuring startup for several environments with many code differences per environment. Typical apps will not need this approach.

To implement environment-based


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

91 classes, create a


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

99 classes and a fallback


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

91 class:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7152
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5105
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample

7

Use the overload that accepts an assembly name:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7152
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5105
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample

8

Startup method conventions

Configure and ConfigureServices support environment-specific versions of the form

dotnet new webapp -o EnvironmentsSample
cd EnvironmentsSample
dotnet run --verbosity normal

04 and

dotnet new webapp -o EnvironmentsSample
cd EnvironmentsSample
dotnet run --verbosity normal

05. If a matching

dotnet new webapp -o EnvironmentsSample
cd EnvironmentsSample
dotnet run --verbosity normal

05 or

dotnet new webapp -o EnvironmentsSample
cd EnvironmentsSample
dotnet run --verbosity normal

04 method isn't found, the


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

95 or


    
Environment is Development
Environment is NOT Development
Environment is: Staging, Development or Staging_2

96 method is used, respectively. This approach is useful when the app requires configuring startup for several environments with many code differences per environment:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7152
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5105
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Path\To\EnvironmentsSample

9

Additional resources

  • View or download sample code ()
  • App startup in ASP.NET Core
  • Configuration in ASP.NET Core
  • ASP.NET Core Blazor environments

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.