Dot Net For All

Breaking Down the ASP.NET core project and WebHostBuilder

Hello, In this article I will break down the ASP.NET core project and understand it in chunks. Although we can always start by creating a full fledged MVC supported project by selecting the template in the start of creating a project. In this article I have created a ASP.NET core project and tried to execute it.

As seen in my previous article I will create an empty template which will help to us better understand the working of the ASP.NET core application.

But whats happening here. If we go and have a look at the folder and file structure of the project. It will look like the following figure.

As we can see in the figure there are two .cs files i.e. Program.cs andd Startup.cs. I have already discussed about the wwwroot folder in this article. Now lets see the contents of the program.cs and startup.cs files.

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

            host.Run();
        }
    }

And as we can see in the above code snippet. The Program.cs contains a Main() method just like the console application. And this main method is also the starting point of the application.

Surprised ! but it is true as ASP.NET core application behave as console applications unlike previous versions of ASP.NET. The previous version used to execute under the IIS context and all the handling of the application was taken care by IIS.

But this is not true with the ASP.NET core. As a console app can live in IIS, apache or any other OS or it can be run directly from
the console in container or linux image and gives us flexibility where and how we are gonna host our web application.

WebHostBuilder – This class is used in the project to listen to the network ports for web request to come in and we have to direct the class to perform something with web request. It helps to create a host for the web application.

UseKestrel – This is the extension method written for the WebHostBuilder class which helps to handle the request by an cross platform open source server known as Kestrel. This web server helps to listen the requests which are originated from internel network but if we need to expose the application to the internet we need to have IIS, apache, or Ngninx as the reverse proxy server.

UseContentRoot – This method helps to locate the file and folders which the project should use to server to the client. These are basically the MVC files and folders.

UseIISIntegration – This method helps to integrate IIS as the reverse proxy server for the Kestrel. Below are very good article on the microsoft sites to understand how the handling of request happen between ketrel and IIS.

Below are the two links where you can learn more about the kestrel and role of IIS in the web project.

Web Server implementation in ASP.NET core

Introduction to ASP.NET Core module

UseStartup – This method helps to identify the startup class for project. In the above code snippet the startup class is StartUp.cs

and Build() method builds an instance of type IWebHost by using all the plumbing provided by the previous methods.

Now lets check the code of the Starup.cs class.

As we can see in the figure above the Configure method takes the context of the web request writes content to it and return the response.

Serving the Static HTML Files in ASP.NET Core

Add a static file named index.html under the wwwroot folder. If you want to know more about the wwwroot folder you can visit this article on my blog. The structure of my project looks as shown below.

And the HTML of the file is simple as shown below.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hell0 Dear</title>
</head>
<body>
<h3>My Hello World</h3>
</body>
</html>
Hell0 Dear

 

My Hello World

Now there can be case where we want to serve some static HTML files to the client. Now there is a way to achieve the same. If we want to server html static file we have to call the UseStaticFiles method.

But as you can see in the method, the visual studio code is showing some compile time error stating that the method does not exits. We need to add all the references to ASP.NET core project using nuget. For this functionality to work we need to add a nuget package named Microsoft.AspNetCore.StaticFiles which can be added to the project from this location.

Once the package is added we can see that the error is gone and if we run the project we cannot see the output in html window. And there is catch which I will discuss a bit later.

Now there is another way to add the nuget package. Right click on the project and choose Edit .csproj file as shown in the figure below. Previous versions of the ASP.NET this was not possible where we had to unload the project and then edit the same file.

And in the left side of the figure we can see the contents of the file. There we can see a group named ItemGroup and if you want to add any nuget reference we can directly update the section and visual studio will add the reference at compile time.

If we run the project you will be surprise that still the contents of the index.html files are not visible. You need to explicitly provide the path of the file as shown in the figure below.

Now this is something which we don’ t want. I should not specify the path of the index.html file every time I want to display it.

There was something in previous versions of ASP.NET where IIS used to take care of the default files in the server. But to accomplish the same in ASP.NET core we need to add UseDefaultFiles() method. This method helps to retrieve Index.html or .htm , default.html or .htm files by default.

To overcome this shortcoming I need to add one more method(UseDefaultFiles) to the asp.net core pipeline as shown in the code below.

       // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseDefaultFiles();
            app.UseStaticFiles();
        }

In the above code the two methods should be present in the same order. As this is the order of the middle ware execution.

That’s it for this article and I hope I have made some important points about ASP.NET core framework clear.

Master you .NET Core concepts

.NET core concepts and security

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview