Skip to content

Instantly share code, notes, and snippets.

@mapfel
Last active August 2, 2018 14:20
Show Gist options
  • Save mapfel/b48dd28b0a708d50260d61a33911aeef to your computer and use it in GitHub Desktop.
Save mapfel/b48dd28b0a708d50260d61a33911aeef to your computer and use it in GitHub Desktop.
ASP.NET Core - Configurations
// https://exceptionnotfound.net/working-with-environments-and-launch-settings-in-asp-net-core/
// https://github.com/exceptionnotfound/DotNetCoreEnvironmentsDemo/blob/master/src/CoreEnvironmentBehaviorDemo/Startup.cs
namespace CoreEnvironmentBehaviorDemo
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) // add an environment-specific config
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else if (env.IsEnvironment("Testing")) //How we use a custom environment value
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
...
// https://docs.microsoft.com/de-de/aspnet/core/fundamentals/environments
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2"))
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment