Building Your First ASP.NET Core MVC App

This article helps to get started with ASP.NET Core MVC quickly. It describes how to get the latest ASP.NET MVC version and create a simple ASP.NET MVC project in Visual Studio 2015, and explains how a basic ASP.NET MVC application works.

What is .NET Core?

Let’s start with some theory..NET Core is an open-source modular platform for creating applications and services that run on Windows, Linux and Mac. It supports multiple languages, like C#, VB, F#, and modern solutions, like generics, LINQ (Language Integrated Query), async development and more.

What is ASP.NET Core?

ASP.NET Core is great redesign of classic ASP.NET. It’s open source and cross-platform framework for building internet connected applications (web, cloud, IoT, mobile backend, etc). It can run on .NET Core or on full .NET Framework. It’s also modular so it introduces minimal overhead as you can take only components you really need.

What is ASP.NET Core MVC?

ASP.NET Core MVC is a web application framework based on ASP.NET Core, which implements the model-view-controller pattern. MVC is a composition of three logic layers:

  • Model – business layer
  • View – display/presentation layer
  • Controller – input control

A model represents application state. A controller handles interactions and modifies the model. The controller also passes information to a view. The view takes necessary information and presents it to a user – renders interface.

Environment setup

I’ll use a free edition of my favorite IDE (Integrated Development Environment) – Visual Studio 2015 Community. It can be used by individuals developing commercial or non-commercial products. You can get it from Visual Studio Download Page.

Next step is to install .NET Core SDK – visit .NET Core installation guide and click ‘Download .NET Core 1.0.1 tools Preview 2’ (or later if available). Close all instances of Visual Studio and run the installer.

dotnet-core-install

The installation should finish in a minute.

dotnet-core-install-success

Create First ASP.NET Core Project

Open Visual Studio and click File / New / Project in the menu.

new-project

Then select your application template. The .NET Core SDK added new templates – you can find them under Templates \ Visual C# \ .NET Core – select ‘ASP.NET Core Web Application (.NET Core)’.

Remember to fill in name and location of your application.

app-location

Next, you will be asked about ASP.NET Core template. For the educational purposes let’s start with Empty template. This way you will better understand the modularity of ASP.NET Core.

Please clear the ‘Host in the cloud’ check box as it’s not needed in this tutorial.

host-in-cloud

Run ASP.NET Core Application

Once the project is created, you can run it just by pressing F5 or clicking following button.

iis-express-button

A web browser should be opened and you should see Hello World message.

browser-result

How ASP.NET Core works?

Now, let’s have a look at what exactly happened. There is Program.cs file in Solution Explorer.

program-cs-explorer

When the Main method is executed, first WebHostBuilder is initialized.

Then Kestrel is specified as a server to be used by the web host.

Next content root directory is set to current directory.

Then IIS integration is enabled – it configures port and base path the server should listen on.

The line that defines the startup type is very important; we will describe it in more details shortly.

Finally, the web host is built and assigned to the host variable.

At last, the web host is run and the thread is blocked until host is shutdown.

Ok, so this all is responsible just for starting the web host. But what is responsible for providing the content?

What is ASP.NET Core Startup Class?

Every ASP.NET application requires at least one startup class. It configures the request pipeline that handles all requests that are sent to the application.

Startup class must have a Configure method that specifies how ASP.NET will respond to HTTP requests.

In the generated project it adds logging to the console.

Next, it turns detailed exception reporting for local development.

Finally, it defines constant response for any request – the reason you see ‘Hello World!’ message when the application is started.

Startup class can have also a ConfigureServices method that is used to include features that must be added before they are wired up in the request pipeline. The method is empty in generated project.

Enable MVC

Now you know how a raw ASP.NET Core application works. Let’s try to enable MVC framework. First we need to add a NuGet package that adds MVC support – right click on the project and select ‘Manage NuGet Packages…’.

manage-nuget-pacs

Click ‘Browse’ in the new tab, then type ‘Microsoft.AspNetCore.MVC’ in search box. Select the package and click ‘Install’.

browse-button

Next let’s add MVC in Startup.ConfigureServices method.

Then add it to request execution pipeline – update Startup.Configure method.

So far so good, but if you run the application you will still see the same ‘Hello World!’ message. It’s because we have no MVC objects defined.

Add First MVC Controller

First, let’s create the Controllers folder.

new-folder

Next, let’s create the HomeController class.

new-class

Put the following code in the file.

If you run your application and append the URL with ‘/home/index’ you should see a new message.

url

Hurray, something more than ‘Hello World!’

Add First MVC View

Let’s work a little bit on presentation of your application. Add one more directory to the project – Views, then create Home subdirectory. Next, add index.cshtml to the Home subdirectory with following content.

new-index

Last thing is to update Controller to return the View result.

When you run the application, you should see the nice message.

app-message

Summary

ASP.NET Core is a very flexible platform that follows modern patterns, like dependency injection. This way it can be easily adjusted to various scenarios.

In this tutorial we have shown only the very basics of ASP.NET Core functionality. Full application sources are available on GitHub.

To continue studying ASP.NET Core, you may visit its website where you can find a number of more advanced tutorials and interesting articles.

Related articles:

ASP.NET Core 1.0. Part 1: Introduction, general description and the future of .NET Framework

ASP.NET Core 1.0. Platform. Part 2. ASP.NET Today or what to choose?

Mariusz Bojkowski

Mariusz Bojkowski

Mariusz is software developer and architect. His commercial adventure with software started in 2006, but it was his PASSION even before. He has blog about C# / .NET development.
Mariusz Bojkowski

Latest posts by Mariusz Bojkowski (see all)

Mariusz Bojkowski

Mariusz is software developer and architect. His commercial adventure with software started in 2006, but it was his PASSION even before. He has blog about C# / .NET development.