In this tutorial, I will show you how to create a new project in ASP NET Core 2.0 with MVC (Model, View, Controller) template using visual studio 2017. And after that, I will discuss the project folder structure of ASP NET Core 2.0 application. If you want to know what is ASP.NET Core then click here.
In previous of this ASP NET Core tutorial series, I have discussed the below topics.
- Introduction to ASP NET Core 2.0 and benefits of ASP.NET Core.
- How to create a new empty project in ASP.NET Core 2.0
What will you learn in this ASP NET Core tutorial?
- How to create a new project in ASP NET Core 2.0 with MVC template using Visual Studio 2017.
- Project folder structure of ASP NET Core 2.0 Application.
Let’s start how to create a new project using MVC template in ASP NET Core 2.0
How to create a project using MVC Template in ASP NET Core 2.0
Step 1: Open visual studio => click on file => select New => then choose project. It will open a new popup window with the name of a new project.
Step 2: Then, first of all, choose web under Visual C# from left pane => then choose “ASP.NET Core Web Application” => then enter the name of the project like “StudentRecord” => then click browse button to choose the location of your project => and then click ok button. After that, you will see a new popup window with the name of “New ASP.NET Core Web Application – StudentRecord”.
Step 3: Then select the version of ASP.NET Core from the above dropdown as I have selected “ASP.NET Core 2.0” => then select template as I have selected “Web Application (Model-View-Controller)” => then just click ok button. It will create an ASP.NET Core 2.0 project using MVC template with all the required files.
Step 4: Then you will see all the files and folder in the right pane of visual studio under the Solution Explorer.
Now, let’s start to understand the project folder structure of ASP.NET Core 2.0 Application.
Project Folder Structure of ASP.NET Core 2.0 Application.
In this part of this tutorial, we will go through all the files and folder that were created in when we created the project above part.
Project Configuration File:
First-of-all, we will look at the project configuration file. In order to look, just right click on the project and then choose “Edit StudentRecord.csproj”. It will open a file with the extension of csproj.
Let’s understand this file code.
First-of-all, you see is a property group and that is target framework and the value of target framework is “netcoreapp2.0” which we selected when we created this project.
Then you see PackageReference node under the ItemGroup. So, whenever you will add a new package or package reference, you will see that package or package reference here.
Then you see <DotNetCliToolReference> element that is structured in a similar way as the <PackageReference> element.
Properties Folder:
In this section, we will take look at the properties folder in our project. Click on the properties folder and then you will see a file launchSettings.json file under the properties folder. Open the launchSettings.json file. This file tells the visual studio what to do if we press the run button.
Let’s understand the code in this file.
By default, it has two profiles. Profile starting from line#10.
line#2 to 8, it will configure the IIS settings and you can configure the authentication and URL and the port.
line#11, starting the first profile, it will launch the IIS express which will host the application and start the browser when that will hit the URL. And it will also set the environment. Here in my case is Development environment.
Line#18, starting the second profile, which we have just the project name. It will run the application as command line application
Note: – These are all the properties of the project. You can see these properties by just right click on the project from solution explorer and choose the properties tab. And then choose Debug tab from left pane and here you will see all the properties.
wwwroots:
Then after properties folder, you will see a folder with the name wwwroots. If you open this folder, you will see a number of folders or files like CSS folder, images folder, js folder and etc.
In this folder you would be placing all the static files like CSS, Javascript and also static HTML files. This is the root folder of our application. This code is basically a separation between code files and static files. So, you should not place any C# or Razor file here.
Controllers Folder:
In this folder, we will add all the controllers of our application as we do in MVC 5. It’s a logic layer of our application.
Model Folder:
In this folder, we will add all the models of our application. It’s a business layer of our application. Model will play with data that we have in the database.
Views Folder:
It will contain all the views of our application. So, we call it as a presentation layer. In this folder, you will see another folder with the name “Shared”. So, inside the shared folder, we will have all the partial views.
If you open the shared folder, you will see the following some files:
- cshtml: It is the master page of our application.
- cshtml: It is the partial page to showing error view.
- cshtml: The code of this file will tell the visual studio the “_Layout” file is the master page of application.
- cshtml: This file contains all the tag helpers. We will discuss tag helpers later in this ASP.NET Core tutorial series.
BundleConfig.json:
This file is responsible for all the bundling and minification. E.g. Input file for style sheet is site.css and the output file name is site.min.css. And same as a javascript file. So, you can add multiple files here and you can make your application more efficient by reducing the size of the static files.
Program.cs:
This file is responsible for starting your application. If you open this file, you will see the main method. When you run the application, it will look this main method and call it the application start.
Startup.cs:
This class is the base class and not driving from any other class. The runtime will call two methods called configure services and configure. So first-of-all, it will call the configure services and then it will call the configure method.
ConfigureServices(): The role of this method is to configure the dependency injection. And the dependency injection is an integral part of ASP.NET Core application. We will add the services in this method like if we want to add DB Context to connect database using entity framework, then we have to define services in this configure service.
Configure(): This method configures the HTTP request pipeline of asp.net core. The pipeline specifies how the application should respond to HTTP request.
Conclusion:
So, we have covered all the files and described the responsibility of every file in ASP NET Core 2.0 Application.
Leave a Reply