Click here to Skip to main content
15,894,180 members
Articles / Web Development / ASP.NET / ASP.NET Core

ASP.NET Core 2.0 Empty Project

Rate me:
Please Sign up or sign in to vote.
1.36/5 (3 votes)
1 Sep 2017CPOL2 min read 3.8K   1  
Create an empty ASP.NET Core project that doesn’t include default features i.e. an empty shell. Continue reading...

Problem

Create an empty ASP.NET Core project that doesn’t include default features, i.e., an empty shell.

Solution

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):

C#
public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup()
                .Build();
    }

    public class Startup
    {
        public Startup(
            IHostingEnvironment env,
            ILoggerFactory loggerFactory,
            IConfiguration config)
        {

        }

        public void ConfigureServices(
            IServiceCollection services)
        {
            // setup dependency injection in service container
        }

        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env)
        {
            // setup request pipeline using middleware
        }
    }

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 for 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 appsettings.json and Environment Variables to IConfiguration, which is available via Dependency Injection. See next section.
  6. Logging: Adds a Console and Debug logging providers.

Once configured, we Build() and Run() the host, at which point the main thread is blocked and host starts listening to 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 request pipeline for our application, using 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 listing 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
-- There are no messages in this forum --