ASP.NET Core Project Structure

Introduction

In this article, we will learn how Visual Studio organizes the ASP.NET Core project structure and understand the project files. Please, read our previous article before proceeding to this article where we learn the CRUD Operations With ASP.NET Core MVC Using ADO.NET.

ASP.NET Core Project Structure

Now, that we have created the Project Structure let’s go-to solution explorer and see what files are different.

If you selected the project using Razer there will be some folders that will be different. Initially, the Project Structure that we created for razor pages and did not have the views or the controllers folder but here we have that difference.

Other than that we have some Appsettings.Json, StartUp.cs, Program.cs class file. And also the Models folder looks familiar.

We have the wwwroot folder which will hold the static files. Then we have the properties which have the launchSettings.Json.

Now, let’s take a look at the Controllers folder and the views folder. When you create an application using the MVC architecture. then your Project Structure gets divided into models, views, and Controller.

Properties

The Properties folder contains a file called launchSettings.json.

launchSettings.jsonin asp.net core
launchSettings.jsonin asp.net core

Now, we’ll be taking a look at a launchSettings.json file. this file sets up the different launch environments that Visual Studio can launch using the launch settings feature.

We can create and use custom environments to enable or disable application services based on the environment. The app is running in and we will be using the environment tag helper to change the content based on those environments.

wwwroot

With ASP.NET core that is an introduction to many new things. the wwwroot folder is the root of our website.

wwwroot folder in asp.net core

All the static files within your project will place inside this directory. The Static files such as Html, CSS, Images, and Javascript are assets in an ASP.NET core application and they save inside the wwwroot folder.

So, one thing you should always remember is all the static content of your website.

Like in the future if you add any images logos everything should live inside the wwwroot folder.

csproj file

Let’s take a look at the Project Structure configuration file now.

That file is not visible here but if you right-click on the project name you will see added csproj file.

csprojfile in asp.net core
csprojfile in asp.net core

No project file has been there for a long time. but the format of the file and how it works is different in ASP.NET Core 2.2 in the initial version of asp.net core.

We had files called Project.Json. the new csproj file replaces both of these files.

So, the first thing you see is a targeted framework which is dotnetcore2.2. The target framework.

After that, you see a package reference entry inside the item group and the first package is Microsoft.AspNetCore.App. This is also called a meta-package. This meant that the package includes all the packages supported by the Asp.Net Core team.

Views

In the views folder, the razor pages have introduced ASP.NET Core 2.0. Razor Pages is a new feature of ASP.NET Core that makes the coding page focused on our use easier and more productive. They provide a simpler way to organize code with ASP.NET Core applications. Keeping implementation logic and view models closer to the view implementation code raising pages is not for simple scenarios. Everything that you can do with MVC application you can do the same by using Razor pages.

Program.cs file

In classic ASP.NET Code in the System.Web Assembly takes care of starting the application. the global.asax had the methods in which you could provide the custom logic. but with ASP.NET core we do not have the global.asax anymore.

The steps needed to start an application are now determined by you and that starts with the program class.

Program.cs file in asp.net core

The Program.cs class contains the main method, which is the entry point for the application when runtime executes the application. It looks for this method and calls it. most .Net applications start using this main method.

The application initially starts as a command-line application. The main method Configure is ASP.NET core and starts it.

StartUp.cs file

The program.cs class executes the runtime it, means which among other things configures the startup class file.

So, the startup class file configures services and the applications request pipeline.

The startup class file includes the ConfigureServices method to configure the application services. Even though this is optional the startup class file. It also includes a configure method to create the applications.

here, we see it is configuring our startup class file. The first, configure services and the next one is to configure the services is to configure Dependency Injection.

startup.cs in asp.net core
startup.cs in asp.net core

This method at the services to the application to make them available. you get the service collection the object that injects inside this matter as a parameter. And now you can use this to build on the services, that would be available to this application. for example of the services would be entity framework core identity services MVC and many more.

The other method is the configure method. This method configures the HTTP request pipeline off ASP.NET Core. So, the pipeline specifies how the application should respond to HTTP requests.

A pipeline composes of individual parts called middleware. When your application receives a request from the browser. it goes through the pipeline and comes back.

Thank you for reading this article, I hope you will understand the ASP.NET Core Project Structure. We’ll get into more details about this when we’ll learn ASP.NET Core InProcess Hosting in the next article.

Leave a Reply

Your email address will not be published. Required fields are marked *