Basics Of ASP.NET Core Using Visual Studio 2017 - Part One

Introduction

This article explains the basics of ASP.NET Core using Visual Studio 2017 and easy ways to create ASP.NET Core applications.

.NET Core

.NET Core is an open source and cross-platform subset of the full .NET framework and it supports Windows, Linux, and macOS. It is used to develop Windows applications as well as Console applications, and it only supports single app model.

ASP.NET Core

ASP.NET Core is a new generation of ASP.NET. It is used to develop web applications with better and improved features. ASP.NET Core is free, open source, and cross-platform development framework. We can develop and run ASP.NET Core apps on Windows, Mac, and Linux. ASP.NET Core is not based on System.Web.dll, it is fully different. There are three important .NET Core tools as follows.

  • Visual Studio 2017
  • Visual Studio Code
  • Command Line Interface.

We can install Visual Studio 2017 in very easy way following the steps mentioned in the article Install Visual Studio 2017.

Simple example and overview of ASP.NET Core Application

We are using Visual Studio 2017 for developing ASP.NET Core application. Open Visual Studio, open New Project >> “ASP.NET Core Web Application (.NET Core)”.

ASP.NET Core

We selected “ASP.NET Core 1.1” and then Empty template. An empty project template is selected for creating an ASP.NET Core application. This template does not have any content in it.

ASP.NET Core

Solution Explorer                             

We can see all the folders and files in Solution Explorer. These files are different as compared to the normal ASP.NET files.

ASP.NET Core

Dependencies are like references in ASP.NET Core. If we expand the dependencies, we can see the NuGet and SDK.  ASP.NET Core is a full NuGet package and it is not based on System.Web.dll.

We can see the SDK and inside SDK, we can see the “Microsoft.NETCore.App”. It is a target to .NET Core framework.

ASP.NET Core

All the NuGet packages are added as references. Right click your project in Solution Explorer and go to Edit DemoASP.NET Core.csproj.

ASP.NET Core

Now, DemoASP.NET.Core.csproj file will open. We can see the package reference in that file which looks like the below screenshot.

ASP.NET Core

If we remove any one of the package references from DemoASP.NET.Core.csproj, it will be removed from NuGet in Solution Explorer too. 

ASP.NET Core
Program.cs

Program class is the main part of the ASP.NET Core. It contains static method and also beginning pointing of the application. This main method creates a host, builds a host, and runs the host.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.AspNetCore.Hosting;  
  7.   
  8. namespace DemoASP.NETCore  
  9. {  
  10.     public class Program  
  11.     {  
  12.         public static void Main(string[] args)  
  13.         {  
  14.             //Create host using WebHostBuilder class  
  15.             var host = new WebHostBuilder()  
  16.                 .UseKestrel()  
  17.                 .UseContentRoot(Directory.GetCurrentDirectory())  
  18.                 .UseIISIntegration()  
  19.                 .UseStartup<Startup>()  
  20.                 .UseApplicationInsights()  
  21.                 .Build();  
  22.   
  23.             host.Run();  
  24.         }  
  25.     }  
  26. }  

Kestrel

Kestrel is a cross-platform web server for ASP.NET Core.  Kestrel is the web server that is included by default in ASP.NET Core project templates. It supports HTTPS.

  1. public static void Main(string[] args)  
  2.         {  
  3.             var host = new WebHostBuilder()  
  4.                 //Specify Kestrel as the server to be used by the web host  
  5.                 .UseKestrel()  
  6.                 .UseContentRoot(Directory.GetCurrentDirectory())  
  7.                 .UseIISIntegration()  
  8.                 .UseStartup<Startup>()  
  9.                 .UseApplicationInsights()  
  10.                 .Build();  
  11.   
  12.             host.Run();  
  13.         }  

Content Root

Content Root specifies the content root directory to be used by the web host. It gets the current root path of our project. For example, our project root path is http://localhost:53498/.

IIS Integration

IIS configures the port and base path the server should listen to when running behind ASP.NET Core Module. The app will also be configured to capture startup errors.

  1. var host = new WebHostBuilder()  
  2.                 .UseKestrel()  
  3.                 //Specify the content root directory to be used by the web host  
  4.                 .UseContentRoot(Directory.GetCurrentDirectory())  
  5.                 .UseIISIntegration()  
  6.                 .UseStartup<Startup>()  
  7.                 .UseApplicationInsights()  
  8.                 .Build();  
  9.   
  10.             host.Run();  

Build and Run

Build is a Microsoft.AspNetCore.Hosting.IWebHost which hosts a web application. Run is used to run our application. Our application is hosted in the host.

  1. var host = new WebHostBuilder()  
  2.                 .UseKestrel()  
  3.                 .UseContentRoot(Directory.GetCurrentDirectory())  
  4.                 .UseIISIntegration()  
  5.                 .UseStartup<Startup>()  
  6.                 .UseApplicationInsights()  
  7.                 // Build the application  
  8.                 .Build();  
  9.             //Finally Run the host  
  10.             host.Run();  

Startup Class

Startup class is a special class and it does not have any inheritance, interface, and over loading. It is mainly used to start the application.

  1. namespace DemoASP.NETCore  
  2. {  
  3.     public class Startup  
  4.     {  
  5.         // This method gets called by the runtime. Use this method to add services to the container.  
  6.         // For more information on how to configure your application,   
  7.         public void ConfigureServices(IServiceCollection services)  
  8.         {  
  9.         }  
  10.   
  11.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  12.         public void Configure(  
  13.             IApplicationBuilder app,   
  14.             IHostingEnvironment env,   
  15.             ILoggerFactory loggerFactory  
  16.             )  
  17.         {  
  18.             //loggerFactory.AddConsole();  
  19.   
  20.             //if (env.IsDevelopment())  
  21.             //{  
  22.             //    app.UseDeveloperExceptionPage();  
  23.             //}  
  24.   
  25.             app.Run(async (context) =>  
  26.             {  
  27.                 await context.Response.WriteAsync("Hello World!");  
  28.             });  
  29.         }  
  30.     }  
  31. }  

The startup class has two methods - ConfigureServices and Configure. ConfigureServices method is used to configure the services and is called at runtime. We can configure normal services, third party services, third party compounds, tools and filters for MVC. We will see this later.

Configure method is used to configure the HTTP request pipeline.

There are three main arguments as interfaces -  IApplicationBuilder, IHostingEnvironment, ILoggerFactory.  

IApplicationBuilder defines a class that provides the mechanisms to configure an application's request pipeline. IHostingEnvironment provides information about the web hosting environment an application is running in. ILoggerFactory represents a type used to configure the logging system and create instances of ILogger from the registered ILoggerProviders.

We are configuring single call using App.Run. App.Run is configuring terminal middleware compound. Middleware compound handles HTTP request.  We are going to do a simple thing here: handle HTTP request and wait for a response to async call so as to write a simple text “Hello World”.

ASP.NET Core
Conclusion

This article explained the basics of the ASP.NET Core.