ASP.NET MVC Folder And File Structure

Introduction

 
I am going to discuss the auto-generated ASP.NET MVC Folder and File Structure when we create a new ASP.NET MVC application. Nowadays, the ASP.NET MVC framework has become more popular among the developers because of the separation of concerns (codes) and folder structure. As a developer, it is very important for you to understand the need and use of each folder and file of an ASP.NET MVC application.

What is the separation of concerns in ASP.NET MVC?

 
It is a process of breaking the program into various distinct features which overlap in functionality as little as possible. MVC pattern focuses on separating the content from presentation and data-processing from the content. Let's understand it with an example.
 
Step 1
 
Open Visual Studio 2015 or a version of your choice and create a new project.
 
Step 2
 
Choose the "web application" type for the project and give an appropriate name to your project.
 
ASP.NET MVC Folder And File Structure 
 
Step 3
 
Select MVC template below and click OK.
 
ASP.NET MVC Folder And File Structure 
 
Step 4
 
Check the auto-generated folder and file structure of the created project.
 
ASP.NET MVC Folder And File Structure 
 
Let’s start discussing the use and need of each Folder and File of an MVC application in details one by one.
 
App_Data
 
The App_Data folder of MVC application is used to contain the application related data files like .mdf files, LocalDB, and XML files, etc. The most important point that you need to remember is that IIS is never going to serve files from this App_Data folder.
 
App_Start
 
The App_Start folder of MVC application is used to contain the class files which are needed to be executed at the time the application starts. The classes like BundleConfig, FilterConfig, IdentityConfig, RouteConfig, and Startup.Auth etc. are stored within this folder. So, in simple words, we can say that configuration related class files are stored in App_Start.
 
ASP.NET MVC Folder And File Structure 
 
Content
 
The Content folder of an MVC application is used to store the static files such as the image files, CSS files, and icons files. When we create an MVC 5 application, by default the bootstrap.css, bootstrap.min.css, and Site.css files are included by Visual Studio, as shown in the image below.
 
ASP.NET MVC Folder And File Structure 
 
Controllers
 
The Controllers folder in MVC application is used to contain all the controllers of your application. The Controller is basically a class which is inherited from the base Controller class. The name of the Controller should end with the word Controller. It is the class which actually handles the user’s request and returns a response.
 
ASP.NET MVC Folder And File Structure 
 
Fonts
 
The Fonts folder of an MVC application contains the custom font files that are required for the application.
 
ASP.NET MVC Folder And File Structure 
 
Models
 
The Models folder of an MVC application is used to store the class files which are used to store the domain data as well as business logic to manage the data.
 
Scripts
 
The Scripts folder of MVC application is used to contain all the JavaScript files that are required for the application. When we create an MVC 5 application, by default the necessary JavaScript files for jQuery and bootstrap are included. If you want to create any custom JavaScript files then it should be created within this folder or any subfolder of this folder. This is not mandatory but it is a good programming practice as at a later time you can easily find out the JavaScript files.
 
ASP.NET MVC Folder And File Structure 
 
Views
 
The Views Folder of an MVC application is used to contain the .cshtml files for the application. In MVC, the .cshtml file is a file where we need to write the HTML code along with the C# code.
 
The Views folder in MVC applications includes separate folders for each and every controller for your application. For example, all the .cshtml files of the HomeController will be in the View => Home folder.
 
We also have the Shared folder under Views folder. The Shared Folder in MVC is used to contain all the views which are needed to be shared by different controllers e.g. error files, layout files, etc.
 
ASP.NET MVC Folder And File Structure 
 
Now, let us discuss the configuration files which are created by the framework by default
 
Global.asax
 
The Global.asax file in an MVC application allows us to write code which we want to run at the application level, such as Application_BeginRequest, Application_error, Application_Start, session_start, session_end, etc.
 
Packages.config
 
The Packages.config file in MVC application is managed by the NuGet which will keep track of what packages and versions have installed within your application.
 
Web.config
 
The Web.config file of an MVC application is one of the most useful and important files which contains the application level configurations.
 

Summary

 
In this article, we discussed the ASP.NET MVC Folder and File structure. Once you understand the need and use of each folder and file of an MVC application, it is easy for you to find, store, and organize the project related files.


Similar Articles