ASP.NET Core 2.0 Empty Project

In this article, we will create an empty ASP.NET Core project that doesn’t include default features, i.e., an empty shell.

Step to be followed

First, create a new empty project using Visual Studio 2017.

  1. File > New > Project
  2. Under “.NET Core”, select “ASP.NET Core Web Application”. Enter Name and Location. Click OK.
  3. Select “Empty”. Click OK.

Next, remove the code from Program.cs and Startup.cs so that they look like below (keeping the necessary "using" statements).

  1. public class Program {  
  2.     public static void Main(string[] args) {  
  3.         BuildWebHost(args).Run();  
  4.     }  
  5.     public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args).UseStartup().Build();  
  6. }  
  7. public class Startup {  
  8.     public Startup(IHostingEnvironment env, ILoggerFactory loggerFactory, IConfiguration config) {}  
  9.     public void ConfigureServices(IServiceCollection services) {  
  10.         // setup dependency injection in service container  
  11.     }  
  12.     public void Configure(IApplicationBuilder app, IHostingEnvironment env) {  
  13.         // setup request pipeline using middleware  
  14.     }  
  15. }  

Discussion

Empty project template in Visual Studio 2017 creates a project with Program.cs and Startup.cs classes.

Program.cs

Just like a Console application, "public static void Main()" is the starting point of ASP.NET Core applications. We’re setting up a host (WebHost) that references the server handling requests (Kestrel). This is achieved using CreateDefaultBuilder() method that configures -

  1. Kestrel- cross-platform web server
  2. Root- content root will use the web project’s root folder
  3. IIS- as the reverse proxy server
  4. Startup- points to a class that sets up configuration, services, and pipeline (see next section).
  5. Configuration- adds JSON and environment variables to IConfiguration available via Dependency Injection (see the next section).
  6. Logging- adds a Console and Debugs the logging providers.

Once configured, we Build() and Run() the host, at which point, the main thread is blocked and the host starts listening to the request from the Server.

When setting up WebHostBuilder, you could set values for various settings via UseSetting()method, which takes in a key/value pair for a property. These properties include applicationName (string), contentRoot (string), detailedErrors (bool), environment (string), URLs (semicolon separated list) and Webroot (string). Some of these properties can also be set via extension methods on WebHostBuilder.

Startup.cs

This class sets up the application dependency injection services and requests pipeline for our application, using the following methods.

  1. ConfigureServices(): add services to the service container
  2. Configure(): setup request pipeline using Middleware.

The parameters for these methods can be seen in the code attachment above.

These methods are called in the same sequence as listed above. The important point to remember about the sequence in which they run is that the service container is available after ConfigureServices, i.e., in Configure() method, and not before that.

Source Code

GitHub - https://github.com/TahirNaushad/Fiver.Asp.Empty

Up Next
    Ebook Download
    View all
    Learn
    View all