Ich benutze dotnet watch
Befehl zum Ausführen des asp.net-Kernprojekts. Standardmäßig wird jedoch Production
als Umgebung übernommen.
Ich habe beide Optionen ausprobiert:
1) > dotnet watch ASPNETCORE_ENVIRONMENT=Development
2) > dotnet run ASPNETCORE_ENVIRONMENT=Development
Aber die Produktion als Umfeld wird immer noch aufgegriffen.
Hinweis: In der Visual Studio-Umgebung ist die Variable in den Projekteigenschaften standardmäßig auf Entwicklung eingestellt und wird von Visual Studio aus ausgeführt, um diese Variable auszuwählen.
Die Frage ist: Wie kann ich ein Dotnet-Core-Projekt in der Entwicklung über die Befehlszeile ausführen ?:
1) dotnet run
2) dotnet watch
ASPNETCORE_ENVIRONMENT
ist eine Umgebungsvariable (und AFAIK) und kein Wechsel zum dotnet
-Cli.
Also, was Sie tun würden, ist es vor mit dem Tool:
rem Windows
C:\> set ASPNETCORE_ENVIRONMENT=Development
C:\> dotnet ...
rem Unix
$ export ASPNETCORE_ENVIRONMENT=Development
$ dotnet ...
Sie müssen keine Umgebungsvariablen verwenden , wenn Sie anpassen, wie WebHostBuilder
seine Konfiguration verarbeitet. Dies ist lediglich die Standardeinstellung für dotnet new -t web
. Wenn Sie beispielsweise in der Lage sein möchten, die Standardumgebung auf "development" anstelle von production zu setzen und das Überschreiben der Umgebung in der Befehlszeile zu vereinfachen, können Sie dies tun, indem Sie das normale Program.cs
Code von diesem ...
public static void Main(string[] args) {
var Host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://0.0.0.0:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
Host.Run();
}
... in so etwas ...
private static readonly Dictionary<string, string> defaults =
new Dictionary<string, string> {
{ WebHostDefaults.EnvironmentKey, "development" }
};
public static void Main(string[] args) {
var configuration =
new ConfigurationBuilder()
.AddInMemoryCollection(defaults)
.AddEnvironmentVariables("ASPNETCORE_")
.AddCommandLine(args)
.Build();
var Host =
new WebHostBuilder()
.UseConfiguration(configuration)
.UseKestrel()
.UseUrls("http://0.0.0.0:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
Host.Run();
}
In diesem Fall funktionieren die Umgebungsvariablen weiterhin, Sie können sie jedoch in der Befehlszeile überschreiben, ohne dass Abhängigkeiten von Drittanbietern bestehen. Beispiel:
dotnet run environment=development
dotnet run environment=staging
Das ist eigentlich was die Yeoman-Generatoren tun .
Sie können die Variable auch inline setzen, wenn Sie dotnet
aufrufen:
ASPNETCORE_ENVIRONMENT=Development dotnet run
Ich habe festgestellt, dass dies ideal für NPM-Skripte ist, muss aber immer direkt vor dotnet
aufgerufen werden, z.
{
...
"scripts": {
"start": "cd MyApp && ASPNETCORE_ENVIRONMENT=Development dotnet run",
"watch": "cd MyApp && ASPNETCORE_ENVIRONMENT=Development dotnet watch"
},
}
Hinweis: Dies funktioniert nur unter OS X oder Linux. Für eine plattformübergreifende Lösung können Sie cross-env
:
npm install cross-env -D
Ändern Sie dann die Skripte in:
{
...
"scripts": {
"start": "cd MyApp && cross-env ASPNETCORE_ENVIRONMENT=Development dotnet run",
"watch": "cd MyApp && cross-env ASPNETCORE_ENVIRONMENT=Development dotnet watch"
},
}
Styling hinzugefügt.
Wenn Sie PowerShell unter Windows verwenden, führen Sie
$Env:ASPNETCORE_ENVIRONMENT = "Development"
Wenn Sie cmd.exe unter Windows verwenden, führen Sie
setx ASPNETCORE_ENVIRONMENT "Development"
und dann Starten Sie die Eingabeaufforderung neu, damit die Änderung wirksam wirdWenn Sie einen Mac/Linux verwenden, führen Sie
export ASPNETCORE_ENVIRONMENT=Development
Überprüfen Sie die Dokumentation
https://docs.Microsoft.com/en-us/dotnet/core/tools/dotnet-run?tabs=netcore21
https://docs.Microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1
dotnet run --launch-profile EnvironmentsSample
launchSettings.json
{
"profiles": {
"EnvironmentsSample": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
},
"applicationUrl": "http://localhost:54340/"
},
}
}
Dank der Antwort von @Technetium ist hier ein Update auf dotnetcore 2.0, das für mich wirklich gut funktioniert:
public class Program
{
private static readonly Dictionary<string, string> defaults = new Dictionary<string, string> {{ WebHostDefaults.EnvironmentKey, "Development" } };
private static IConfiguration config;
public static void Main(string[] args)
{
config = new ConfigurationBuilder()
.AddInMemoryCollection(defaults)
.AddEnvironmentVariables("ASPNETCORE_")
.AddCommandLine(args)
.Build();
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
/* Following setting already accounted for in CreateDefaultBuilder() : https://docs.Microsoft.com/en-us/dotnet/api/Microsoft.aspnetcore.webhost.createdefaultbuilder?view=aspnetcore-2.0 */
// .UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
}
Nochmals vielen Dank, @ Technetium