Configuring an ASP.NET Core MVC Application

Configuring an ASP.NET Core MVC Application

Following on from my post on how to upgrade an ASP.NET 5 application to .NET Core, here's how to configure it.

By default a .NET Core web application will listen on port 5000 and will run it as a production environment. We may want to run two applications at the same time, or we may want to run an application in a development environment so we can show more developer-friendly error messages.

Here is our starting point for launching the application

using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;

namespace TheObjectifiedProgrammer
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

We don't always want to run in production mode. We may want more verbose logging while developing, we would definitely not want detailed errors being presented to end users on a live website. This would be set in the configure method of our Startup class which may look something like the following

public void Configure(IHostingEnvironment env, ILoggerFactory loggerFactory)
{
  if (env.IsDevelopment())
  {
    app.UseDeveloperExceptionPage();    
    loggerFactory.AddConsole(LogLevel.Debug);
  }
  else
  {
    app.UseExceptionHandler("/Home/Error");
    loggerFactory.AddConsole(LogLevel.Warning);
  }
/* ... snip ... */
}

So how do we get env.IsDevelopment() to return true? We can set what mode we want to run the application in by setting an environment variable before calling dotnet run.

export ASPNETCORE_ENVIRONMENT=Development
dotnet run

In the same way we can define what URLs the application will listen on

export ASPNETCORE_SERVER.URLS=http://localhost:12541/
dotnet run

This can also be specified while invoking the application which has the advantage of being isolated to the application rather than being set as an environmental variable that could affect other applications

dotnet run --server.urls=http://localhost:12541

But we can also add some logic to set this from the command line. A quick example is included below that will set the application environment and what port it listens on based on the arguments passed to the application.
Obviously a bit more detail and polish would be needed for anything that was ever meant to be used or generally see the light of day.

using System;
using System.Linq;
using System.IO;
using Microsoft.AspNetCore.Hosting;

namespace TheObjectifiedProgrammer
{
    public class Program
    {
        public enum Environment
        {
            Production,
            Staging,
            Development
        }

        public static void Main(string[] args)
        {
            Environment env = Environment.Production;
            
            if (args.Contains("dev"))
            {
                env = Environment.Development;
            }
            else if (args.Contains("staging"))
            {
                env = Environment.Staging;
            }
            
            string urls = "http://localhost:";
            string environment;
            
            switch (env)
            {
                case Environment.Development:
                    urls += 5002;
                    environment = "Development";
                    break;
                case Environment.Staging:
                    urls += 5003;
                    environment = "Staging";
                    break;
                case Environment.Production:
                default:
                    urls += 5004;
                    environment = "Production";
                    break;
            }
            
            var host = new WebHostBuilder()
                .UseUrls(urls)
                .UseEnvironment(environment)
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

An example of usages would be

dotnet run # will default to Production
dotnet run dev # will run as a Development environment
dotnet run staging # will run as a Staging environment