
Getting started with ASP .NET Core
ASP .NET Core is an open source framework that allows to build đȘ cloud-based applications such as web servers, IoT and mobile APIs / backbends. In this article, we will create together our first âhello worldâ web app using the Visual Studio Code IDE.
Please note that this framework is different from the ASP .NET framework. If you are looking for ASP .NET, then this article is not for you đ±.
Prerequisites
Download and install the .NET Core SDK :
We will be using .NET Core 2.0 PREVIEW 2 which is the latest stable version at the time of writing.
Install Visual studio code and these C# extensions:
đ We are ready to create and run a âHello worldâ web app.
Creating and running a âHello Worldâ web app:
Create a new empty folder and open it with Visual Studio Code. This will be the project folder where we will create and run a web app using only commands đ. Letâs run the following commands
dotnet new web #this will create a new simple web app project
dotnet restore #this command adds all required references
dotnet run #runs a web server that hosts the web app that we just created
You should see a similar output for the last command:
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
The run command start a local web server listening for port 5000. We can now verify if the web app is correctly running by entering the URL http://localhost:5000 on any web browser.

If we try to load a random URL such as http://localhost:5000/testdsfdsf. We can see that we always get the âhello worldâ page. Whatâs going on here đ¶? Letâs analyse the code before enhancing it.
Analysing the code
Letâs look at the code crated by the âdotnet newâ command. There, are two source files: Program.cs and Startup.cs. The Program.cs file contains the Main function which does the following:
The Main function basically builds a IWebHost instance using WebHostBuilder.The builder first sets the following things
- .UseKestrel(): use Kestrel web server: as you wiil see ASP .NET Core needs to run on top of a server. In this case, it is the Kestrel web server which is shipped with .NET Core. Kestrel is a cross-platform HTTP server based on libuv, a cross-platform asynchronous I/O library.
- .UseContentRoot(Directory.GetCurrentDirectory()): sets the current directory as content root path used by the web host.
- .UseIISIntegration(): ignore this for now ^^.
- .UseStartup<Startup>(): The class that is used when the server starts. The name of that class can be anything we want, but it is called Startup by convention. The Startup class allows to configure the web server. We will see that more in detail later
- .build(): build a IWebHost instance
Once the builder finishes creating the IWebHost instance, the next instruction starts the web server.
host.Run();
Next, we will look at the Startup class. Particularly the âConfigureâ method
The âConfigureâ method allows to configure the HTTP request pipeline. It does the following:
- Sets the logger
- Sets the development exception page in the pipeline
- Returns a simple body that contains the text âHello Worldâ. The same body is always returned regardless of the http request as long as it targets the server that we created.
The culprit of giving us static pages is this part which always writes the same content to the response body.
This basic Startup class is not really interesting since it will always create the same static page. In the next section, we will make the web site more dynamic by configuring the pipeline.
Dynamic pages using Razor Pages
In the part, we are going to make the web site more dynamic. In order to achieve this, we will modify the existing Startup to use Razor Pages instead of the static writeAsync.
Razor Pages is a feature that allows to simplify the creation of web pages. It works somewhat like XAML in the way that you can define the page content and its code behind. The content and the code behind can be in the same file with cshtml extension or in separate cshtml and cs files.
In order to activate Razor Pages, we need to add the Mvc service in the âConfigureServicesâ method:
After that, add the service to pipeline replacing the writeAsync line.
Once the service is activated, we can add pages that will be handled by Razor Pages. They will be located in the âPagesâ folder in the project.
In order to add in âindexâ page do the following:
- Create a folder named Pages in the root directory of the project
mkdir Pages
- In the âPagesâ folder Create a file called âIndex.cshtmlâ and paste the following content:
- Now, run the web server again and open the page http://localhost:5000/ or http://localhost:5000/index

Yesss đ! , the page displays come dynamic content. However, if we try to open any other URL, we get a blank page. Thatâs because Razor Pages maps the page address to its location in the âPagesâ folder.
To illustrate this, we will add a â/about/Contactsâ page and. we will also separate the code behind and the page in separate files.
- Create the folder âaboutâ inside the âPagesâ folder
- Create two files: Contacts.csthml and Contacts.cs. They will be respectively the contacts page and its code behind file.
- Put the following code in Contacts.cshtml. It is a page that shows a title and two properties defined in the code behind.
- The code behind defines the two properties used in the page. The property Message is dynastically updated when the page is requested.
- We can open the new page by entering http://localhost:5000/about/contacts (because it is located in the Pages/about folder in the project)

Conclusion
This was an introduction to ASP .NET Core. As we have seen, it is very easy to setup and run a web app. We can do so much more with ASP .NET Core. For example, we can use an entity framework database in our web app to add persistence.
I strongly encourage you to read the official documentation and play with the examples.
Happy coding.
