Utilizing the ASP.NET Core Atmosphere Function to handle Growth vs. Manufacturing for any config file sort

by admin

ASP.NET Core can understand what “environment” it’s running under. For me, that is “improvement,” “check,” “staging,” “manufacturing,” however for you it may be no matter makes you cheerful. By default, ASP.NET perceive Growth, Staging, and Manufacturing.

You may the change how your app behaves by asking “IsDevelopment” to do sure issues. For instance:

if (env.IsDevelopment())

if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2"))

There are helpers for the usual environments, or I can simply move in a string.

It’s also possible to make Environmental choices with taghelpers like this in your Views/Razor Pages. I did this once I dynamically generated my robots.txt files:

@web page
Structure = null;
this.Response.ContentType = "textual content/plain";
# /robots.txt file for http://www.hanselman.com/
Consumer-agent: *
<surroundings embody="Growth,Staging">Disallow: /</surroundings>
<surroundings embody="Manufacturing">Disallow: /weblog/non-public
Disallow: /weblog/secret
Disallow: /weblog/somethingelse</surroundings>

This can be a very nice option to embody issues like banners or JavaScript when your website is operating in a sure surroundings. These are simply set as surroundings variables in the event you’re operating in a container. For those who’re operating in an Azure App Service you set the surroundings from the Config blade:

Now that I’ve moved this blog to Azure, we have now a variety of config information which might be particular to this weblog. For the reason that configuration options of ASP.NET are so versatile it was simple to increase this concept of environments to our personal config information.

Our Startup class units up the filesnames of our varied config information. Word the second line, if we have now no surroundings, we simply search for the common file identify.

public Startup(IWebHostEnvironment env)
hostingEnvironment = env;

var envname = string.IsNullOrWhiteSpace(hostingEnvironment.EnvironmentName) ?
"." : string.Format($".{hostingEnvironment.EnvironmentName}.");

SiteSecurityConfigPath = Path.Mix("Config", $"siteSecurity{envname}config");
IISUrlRewriteConfigPath = Path.Mix("Config", $"IISUrlRewrite{envname}config");
SiteConfigPath = Path.Mix("Config", $"website{envname}config");
MetaConfigPath = Path.Mix("Config", $"meta{envname}config");
AppSettingsConfigPath = $"appsettings.json";


This is the information in my Visible Studio. Word that one other good thing about this naming construction is that the information nest properly beneath their mum or dad file.

Nested config files

The formalization of environments just isn’t a brand new factor, however the adoption of it deeply into our utility at each stage has allowed us to maneuver from dev to staging to manufacturing very simply. It’s totally possible that you’ve got performed this in your utility, however you might have rolled your individual answer. Have a look in the event you can take away code and undertake this in-built approach.

This is some articles I’ve already written with regards to shifting this weblog to the cloud:

For those who discover any points with this weblog like

  • Damaged hyperlinks and 404s the place you would not count on them
  • Damaged photos, zero byte photos, big photos
  • Normal oddness

Please file them right here https://github.com/shanselman/hanselman.com-bugs and let me know!

Sponsor: Affected by a scarcity of readability round software program bugs? Give your prospects the expertise they deserve and count on with error monitoring from Raygun.com. Installs in minutes, strive it right this moment!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, advisor, father, diabetic, and Microsoft worker. He’s a failed stand-up comedian, a cornrower, and a e-book creator.

About   Newsletter

Internet hosting By
Hosted in an Azure App Service

Related Posts

Leave a Comment