ASP.NET Core MVC Web Application (Project Structure)

Sena Kılıçarslan
.NET Core
Published in
6 min readMar 30, 2019

In this post, I will make a brief introduction to ASP.NET Core MVC and then explain the project structure of an ASP.NET Core MVC web application. If you are new to .NET Core, you can read my Introduction to .NET Core post first.

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, Internet-connected applications. You can read ASP.NET Core Fundamentals from Microsoft documentation.

ASP.NET Core MVC is a rich framework for building web applications and APIs using the Model-View-Controller (MVC) design pattern.

What is the MVC pattern?

The Model-View-Controller (MVC) architectural pattern separates an application into three main groups of components: Models, Views, and Controllers. This pattern helps to achieve separation of concerns.

The Model in an MVC application represents the state of the application and any business logic or operations that should be performed by it.

Views are responsible for presenting content through the user interface. There should be minimal logic within views, and any logic in them should relate to presenting content.

Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render. In the MVC pattern, the controller is the initial entry point and is responsible for selecting which model types to work with and which view to render.

The following diagram shows the three main components and which ones reference the others:

Source : https://docs.microsoft.com/en-gb/aspnet/core/mvc/overview?view=aspnetcore-2.2

Project Structure of a Web Application in ASP.NET Core MVC

First, we will create a project in Visual Studio. Open File -> New -> Project… and select ASP.NET Core Web Application in the following window:

Give your project a name and then press OK.

In the next window, select Web Application (Model-View-Controller) and be sure that .NET Core and ASP.NET Core 2.2 are selected as shown below:

NOTE: .NET Core 2.2 is the latest stable version as of the moment. If you don’t have this version you can download from here.

After pressing OK, the starter project is created as below:

Now, let’s examine the project structure:

In ASP.NET Core, Dependencies is the place where the necessary dll.s for the application are stored.

Under the Properties menu, you will see a file called launchSettings.json which describes how a project can be launched. It describes the command to run, whether the browser should be opened, which environment variables should be set, and so on.

launchSettings.json

Static content is hosted in the wwwroot folder. The content such as CSS, Javascript files and Bootstrap, jquery libraries need to be included here.

Controller, Models and Views folders are automatically created as we chose Web Application (Model-View-Controller) in the above screenshot. These support MVC pattern and we will add new files to these folders in the following posts while building web applications.

appsettings.json is used to store information such as connection strings or application specific settings and these are stored in the JSON format as the file extension suggests. (If you are familiar with ASP.NET MVC you may notice that the function of this file is similar to Web.config)

appsettings.json

At the bottom there are two classes: Program.cs and Startup.cs.

Program.cs is the main entry point for the application. It then goes to the Startup.cs class to finalize the configuration of the application.

Program.cs

Startup.cs includes Configure and ConfigureServices methods.

Startup.cs

The Dependency Injection pattern is used heavily in ASP.NET Core architecture. It includes built-in IoC container to provide dependent objects using constructors.

The ConfigureServices method is a place where you can register your dependent classes with the built-in IoC container (ASP.NET Core refers dependent class as a Service). After registering the dependent class, it can be used anywhere in the application. You just need to include it in the parameter of the constructor of a class where you want to use it. The IoC container will inject it automatically.

The Configuremethod is used to specify how the app responds to HTTP requests. The request pipeline is configured by adding middleware components to an IApplicationBuilder instance. IApplicationBuilder is available to the Configure method, but it isn’t registered in the service container. Hosting creates an IApplicationBuilder and passes it directly to the Configure method.

The default route for the MVC application is the Index method in the Home controller as specified below in the Configure method:

Index method in the HomeController.cs returns the view Index.cshtml which can be found under /Views/Home/ .

HomeController.cs

The execution sequence of the application is as follows:

Let’s run the project to see how it comes at first. I will use IIS Express as the web server and Firefox as the web browser to run the application:

We get the Welcome page as below:

This is where this post ends. Hopefully, you had a general idea about the structure of an ASP.NET Core MVC Web Application starter project. In the next post, I will show how to build a web application with data access using Entity Framework Core and ASP.NET Core MVC.

Stay tuned :)

--

--

Sena Kılıçarslan
.NET Core

A software developer who loves learning new things and sharing these..