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)”.
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.
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.
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.
All the NuGet packages are added as references. Right click your project in Solution Explorer and go to Edit DemoASP.NET Core.csproj.
Now, DemoASP.NET.Core.csproj file will open. We can see the package reference in that file which looks like the below screenshot.
If we remove any one of the package references from DemoASP.NET.Core.csproj, it will be removed from NuGet in Solution Explorer too.
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.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Hosting;
-
- namespace DemoASP.NETCore
- {
- public class Program
- {
- public static void Main(string[] args)
- {
-
- var host = new WebHostBuilder()
- .UseKestrel()
- .UseContentRoot(Directory.GetCurrentDirectory())
- .UseIISIntegration()
- .UseStartup<Startup>()
- .UseApplicationInsights()
- .Build();
-
- host.Run();
- }
- }
- }
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.
- public static void Main(string[] args)
- {
- var host = new WebHostBuilder()
-
- .UseKestrel()
- .UseContentRoot(Directory.GetCurrentDirectory())
- .UseIISIntegration()
- .UseStartup<Startup>()
- .UseApplicationInsights()
- .Build();
-
- host.Run();
- }
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.
- var host = new WebHostBuilder()
- .UseKestrel()
-
- .UseContentRoot(Directory.GetCurrentDirectory())
- .UseIISIntegration()
- .UseStartup<Startup>()
- .UseApplicationInsights()
- .Build();
-
- 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.
- var host = new WebHostBuilder()
- .UseKestrel()
- .UseContentRoot(Directory.GetCurrentDirectory())
- .UseIISIntegration()
- .UseStartup<Startup>()
- .UseApplicationInsights()
-
- .Build();
-
- 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.
- namespace DemoASP.NETCore
- {
- public class Startup
- {
-
-
- public void ConfigureServices(IServiceCollection services)
- {
- }
-
-
- public void Configure(
- IApplicationBuilder app,
- IHostingEnvironment env,
- ILoggerFactory loggerFactory
- )
- {
-
-
-
-
-
-
-
- app.Run(async (context) =>
- {
- await context.Response.WriteAsync("Hello World!");
- });
- }
- }
- }
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”.
Conclusion
This article explained the basics of the ASP.NET Core.