Getting started with ASP .NET Core

Yassine Benabbas
Jul 22, 2017 · 6 min read

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

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:

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:

mkdir Pages

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.

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.

Links:

Yassine Benabbas

Written by

Mobile app developer at Worldline, teacher and PhD. I love programming, videos games and manga. Trying hard to learn some Japanese.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade