ASP.NET Core is a cross-platform, high-performance, open- source framework and it's interesting to explore. In this article, I’ll cover how to create the basic ASP.NET Core MVC web app and its request processing pipeline, build and deploy to Azure app services. Before I started learning ASP.NET Core, my assumption about ASP.NET Core is, it is totally different from ASP.NET and I need to learn everything from scratch. But after I learnt ASP.NET Core, the answer for my assumption is NO. Why? Because it is pretty similar to ASP.NET request processing pipeline. But here with ASP.NET Core, we decide how application request processing pipeline should be unlike ASP.NET framework which comes with the default (pre-defined) request processing pipeline.
ASP.NET Core & Framework
ASP.NET Core application starting point is a
Main method. Once we create a blank ASP.NET Core application, Program.cs and startup.cs files will be added by default. These are the two key .cs files in the application.
Program.cs file is similar as typical console application file and it’s a default file. There is a
Main method inside that
BuildWebHost method implementation. This is a method which is responsible for configuring web server and other important components of the application pipeline and we can customise according to our requirements.
In this example, I go with the default
CreateDefaultBuilder will register the web server which is kestrel web server unlike ASP.NET which uses IIS as the web server.
UseStartup will register the startup class. This is another important key class file. It has two default methods which are
ConfigureServices method gets called by the runtime. Use this method for adding services to the container and it can be accessed anywhere under the solution. With this method, we can see how ASP.NET Core easily simplified the power of Dependency injection (DI). I'm sure you totally love this design. This method helps to register the service/class once at the start of the application and use it in any class files by just adding the registered service which typically will be a class in most of the cases.
Configure method helps to register the middleware. Middleware is nothing but software that's assembled into an app pipeline to handle requests and responses. Each component:
- chooses whether to pass the request to the next component in the pipeline
- can perform work before and after the next component in the pipeline is invoked
To know more about ASP.NET Core middleware, visit this Microsoft documentation page.
The Configure method is used to specify how the app responds to HTTP requests. The request pipeline is configured by adding middleware components to an IApplicationBuilder instance. Each
Use extension method adds a middleware component to the request pipeline. For instance, the
useMVC extension method adds the Routing Middleware to the request pipeline and configures MVC as the default handler.
I've uploaded the code into github repository. You can download and it's a pretty simple example you can play around with lot more features available in ASP.NET Core libraries.
Razor Pages is a new aspect of ASP.NET Core MVC that makes coding page-focused scenarios easier and more productive. After the Razor pages come, Razor pages are the favorite machanism for creating HTML pages and MVC controllers are for creating web API and restful services. To know details about razor page, click here.
Setting Up a Host
Unlike ASP.NET, ASP.NET Core can be hosted in all different machines includes Windows, Linux and macOS. Regardless machines Kestrel is the web server to host the application. There are two different Host API available for using
- Web Host – Suitable for hosting web apps
- Generic Host - Suitable for hosting non-web apps
ASP.NET Core apps configure and launch a host. The host is responsible for app startup and lifetime management. Create a host using an instance of IWebHostBuilder. This is typically performed in the app's entry point, the
Main method. In the project templates,
Main method is part of Program.cs file. Program.cs calls CreateDefaultBuilder to start setting up a host and it's doing by default.
Create Azure App Service
Create the Azure app service to deploy our first ASP.NET Core application. Azure App Service enables us to build and host web apps, mobile back ends, and RESTful APIs in the programming language of our choice without managing infrastructure. It offers auto-scaling and high availability, supports both Windows and Linux, and enables automated deployments from GitHub, Azure DevOps, or any Git repo.
Login to Azure portal using your own credentials. Choose App Services -> click Add -> from the market place choose Web App -> create. And fill the information as below:
Choose the app service plan according to the environment and the features we need. The standard tier will have the basic features including the option for swapping the deployment slot. We need this feature for this exercise. In the later part of this article, we see how to swap the application slot.
Once the app service is created, then the resource group will look like below:
Application Insights, App service and App service plan will be added after the initial web creation. The default app service
aspdotnetcoreweb is the production environement by default. If you click the
aspdotnetcoreweb app service, the overview page will look as below. And the production URL is "https://aspdotnetcoreweb.azurewebsites.net". We can map the URL with our own custom domains or if we want to purchase the new domain, we can do it here as well. I will leave it to you to do this part, for this example, I am using the default URL created after the web app creation.
when we browse the URL, the default home page is showing. We are going to deploy our ASP.NET Core web application to this URL later.
In app service overview page, I created the deployment slot. Refer to this Microsoft documentation to know how to create the deployment slot.
I created a deployment slot called
dev which is another environment and it's like our SIT/DEV. We can create multiple environments like dev, staging. For this example, I created dev slot only and the resource group looks below after adding.
Note Resource group gets added with a new web app called
dev. And it got the different url from production as "https://aspdotnetcoreweb-dev.azurewebsites.net". Now we have our dev and production environments. We will deploy our web app to dev slot, then we swap it in production. It's a typical process to deploy to staging and test the application and deploy to production.
Build & Deploy the Application to Azure App Service
There are various options to deploy the application to Azure app service. Here is the list of options to deploy ASP.NET Core apps to Azure app service. Refer to this Microsoft documentation page.
Setup the build pipeline to start the build automatically once the code is checked into github repository using Azure pipelines. To learn how to setup the Azure build pipeline, refer to this article. Only difference from that article while choosing the template is choose ASP.NET Core template as the build template instead .NET Desktop template.
After the successful build, deployed the application to dev slot, our first ASP.NET Core application is deployed to dev environment since we choose dev slot as the deployment target.
Swap the Slot to Production
After the verification of application in dev environment, then we move the dev slot to production if the verification is successful.
Here you go! Now the application is deployed to production. It's very easy to swap the deployment slot to production with zero downtime. One of the great features of Microsoft Azure app services.
Points of Interest
I summarised the key features of ASP.NET Core and its implementation and Azure app services. The best place to get more information on each topic, visit the Microsoft documentation page on the ASP.NET Core.
Happy learning! Any improvements to this article, please comment below.