I hope you know that Microsoft has recently launched ASP .NET Core. They have re-written the entire .NET framework. Let’s start how to work with it.
Pre-requisites:
- You have installed .NET core SDK on your local machine. If you haven’t please download it from https://www.microsoft.com/net/download/core#/sdk
- You have installed Visual Studio 2015 or 2017. If you haven’t, please download it from https://www.visualstudio.com/downloads/. For my tutorials, I am going to use VS 2017.
- Of course, a browser 😊
Let’s start our work.
- Create an empty project. So, we can start from scratch. I named it AspDotNetCoreDemo
- Select empty project with latest .NET Core version
Hit OK
- You’ll see the following directory structure:
Now, we’ve an empty project with some must have files. One of them is:
launchSettings.json: This file contains the information about IIS settings and profiles for setting the different environments. Leave it for now. We will discuss this in upcoming blogs.
Let's move to the entry point of our application i.e. Program.cs file. It has a class named Program with an entry point method Main(). Whenever we host our application, Main() executes first and one time only for doing the server related configurations. Here is its code:
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
Let’s discuss each of these methods:
- WebHostBuilder class is responsible for hosting the application and configuring the corresponding server (here we used kestrel).
- UseKestrel(): This method tells the host to use Kestrel server.
- UseContentRoot(Directory.GetCurrentDirectory()): This is to specify the root folder of the application. That’s why we used Directory.GetCurrentDirectory(), this returns the physical path of the current folder. By default, web host search files in the directory from where we run the app using CLI. So, this method is redundant to use if we run the app from the same directory.
- UseIISIntegration(): A .NET core app must be hosted on a Kestrel server or use WebListner/HttSys (not compatible with IIS) or another custom server that implements IServer but hosting on IIS is optional because Microsoft has done this to remove the dependency over IIS and makes it hostable on Linux. It is recommended to use a reverse proxy server for internet apps because most of the work of app management is done by Kestrel and rest of the work are left for IIS (for windows) like max concurrent requests, timeout etc. That’s why this method is recommended to use for internet apps and not for intranet apps.
- UseStartup<Startup>(): We will discuss Startup class in detail about this in our next blog. For now, it is enough to say this method is used to specify the startup class that has two methods:
- ConfigureServices(): For adding the app services to the container.
public void ConfigureServices(IServiceCollection services)
{
}
- Configure(): This is used for injecting the code inside http request pipeline that would be called for every http request.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
- Build(): This is just for building the web host with the supplied configurations. It will throw an exception if anything needed for hosting is not supplied like a server. Try commenting out the UseKestrel() and run the web app.
- Run(): This is for running the web application and block the calling thread until host shutdown.