Getting Familiar with ASP.NET Core
Introduction to ASP.NET Core
ASP.NET Core is a new open-source and cross-platform framework for building modern cloud-based Internet-connected applications, such as web apps, IoT apps and mobile backends. It was architected to provide an optimized development framework for apps that are deployed to the cloud or run on-premises. It consists of modular components with minimal overhead, so you retain flexibility while constructing your solutions. You can develop and run ASP.NET Core apps on Windows, Mac and Linux. ASP.NET Core apps can run on .NET Core or on the .NET Framework.
Create ASP.NET Core project
To start the development of our application. I am going to create a new ASP.NET Core application using visual studio 2015.
Steps: File -> New ->Project
Select the ASP.NET Core and then select empty type. Reason why I selected empty type I just want to use the minimum codes that required to start the project and that is the best way to understand what is going on.
Before we jump in to the development will go through the project structure and specific classes added to the project when we create this.
Once you create the project you can see its added a ASP.NET Core project which is includes Program.cs and Startup.cs. Let’s go and explore those classes.
Program is our starting point of the application. Most of you familier with this because you use aconsole applications before. Asp.Net core project also a console application. More specifically its call ASP.Net specific libraries. In our case, will be running a web application so we need to host this application. Because of that WebHostBuilder instance is initialized.
SO, we are hosting a web application we need a web server.Asp.NET Core completely decoupled from the environment that host the web application. It’s ships with two different http servers.Web listener which is windows only server and Kestrel cross flat form web server. Kestrel is the default but we are using visual studio here so it will use IIS express as web server.
Startup class is the entry point of an application. At this moment its included two methods, ConfigureServices and Configure.Configuration services used to add services to container and configured those services. This container used for dependency injection. We will talk about dependency injection later in this article series. But what are services in ASP.NET Core world. Service is a component that is intended for common consumption in an application. Those services can be built in services or custom service that we are creating during our application development.
The ASP.NET Core Request Pipeline and Middleware
Whenever HTTP request comes in something must handle that request so eventually it’s a result of HTTP response. Those pieces of codes that handle the request and resolved in responses that make up the request pipeline. What we can do is configure that pipeline by adding middleware which are software components that are assembled in to an application pipeline to handle requests and responses. If you worked with older version of ASP.NET you know that module and handlers to care of this for us. In ASP.NET Core those are taken over by middleware. Few samples that we can add to this pipeline are diagnostic middleware or middleware to handle authentication. Even MVC is a piece of middleware that can be added to request pipeline.
Now will look at such a request. The Asp.Net request pipeline consist of a sequence of request delegates called one after the next from one piece of middleware to the next. Each of those can perform operations before and after the next delegates. Important to know that each component choices whether to pass the request to next component in the pipeline or not. So, the order in which we added the middleware to the request pipeline matters. Good example is authentication middleware, if authentication not success request should not pass to the next middleware and immediate response unauthorized request.
Configuring the ASP.NET Request Pipeline
In this section I am going to show you how to configure the ASP.NET Core request pipeline. I am going to show you when we are in the development environment I want to show the developer friendly Error message when exception thrown. To do this actually we no need to do anything its already built in.
Let’s have a look at this statement app.UseDeveloperExceptionPage();
This configures the request pipeline by adding a developer exception page middleware to the request pipeline. So now when exception thrown this piece of middleware will handle it.
To test this, I will do a small change to my code to throw and exception.
If you run the application you can see a nice error page lot of useful information’s.
Now check the code belove carefully and you can identify there is a specific condition check before use the developer friendly error page. That condition used to make sure this developer friendly error only show when application running in development environment.
May be this is a good time to talk about separate environments. Because we might need to execute different piece of code depending on environment and this is a good example of that. The developer exception page that is not something we want to show everyone. We need this during development but not in production.
Let’s have a look debug tab of the project properties.
Here we can find ASPNETCORE_ENVIRONMENT variable its set to development. ASP.NET Core references environment variable hosting environment, to describe the application currently running. Three values are used Development, staging and production.
Now will change our code to handle the error differently when application running in production. What I want is catch the exception and log it. There is a middleware for that as well, the exception handler middleware.
Go to the debug tab and change the ASPNETCORE_ENVIRONMENT to production and run the application you will get the blank page. If you still get the developer error page you need to restart the IIS express.
This is the complete of the part one of the Building Your First API with ASP.NET Core series of article. This covered all the basic information that you should know about the ASP.NET Core.
Code Sample: Part One Code Sample
Next article: Building Your First API with ASP.NET Core Part II – Creating the API and Returning Resources (Coming Soon)