Introduction
MVC stands for Model View Controller. It is a software architectural design pattern. Architectural Patterns provide design reusability and separation of code by providing the solutions to frequently recurring problems. Design Patterns help developers in identifying common problems and provide a predefined way to solve these problems. Design Patterns provide some of the best practices to refactor the code from bad practice to good practice. Patterns are a way to provide a solution for a well-known error or problem in an appropriate manner. MVC was originated by Trygve Reenskaug at Xerox Parc (Palo Alto Research Center) in 1970 as a part of a small task.
Popular Programming Languages and their MVC Frameworks -
- Java-Spring
- Ruby-Ruby on Rails
- JavaScript-AngularJs
- Php-Cake Php
- .Net-Asp.net Mvc
In MVC architectural patterns, application development is divided into three modules.
- Model
- View
- Controller
Models- Model is a set of classes, that is responsible for data and business-related logic. Model is used to communicate with the database and describes how the data can be changed and manipulated.
Views- View describes how the application user interface is displayed. In MVC, Views are created by using C# and HTML Views get the result from the Controller and display the data to the users.
Controllers- Controller is the most important component of MVC. Controllers handle the application execution logic, take input from Views, and send data to Models. A Controller controls the overall execution flow of an application.
Features of ASP.NET MVC
- Separation of concern (SoC)
SoC is a process of dividing application development into different modules. MVC is mainly divided into three components - Model, View, and Controller. It helps in the development process and increases the development speed.
- Parallel Application development
Parallel development is a process of development of an application by multiple developers. In MVC, multiple developers can work on the same application module.
- Test-Driven Development
In a test-driven development, a tester provides the guidelines to developers. MVC applications are easy to modify with the make-big changes.
- Clean URL
MVC provides different routing techniques that do not depend on file systems but Controllers. These URLs are easy to perform SEO.
- Performance Improvement
MVC uses only HTML controls and no server controls like ASP.NET Web Forms. Due to this, the performance is increased. MVC uses HTML controls for UI development, that makes it easy to add client-side scripting languages.
- Improve functionality
In MVC, lots of new concepts like filers, routing etc. are added to make the application development faster and easier.
Folder structure of ASP.NET MVC application
To understand the folder structure of an MVC application, let us create a project. Now, open Visual Studio and create a new project. Let us now check the Solution Explorer.
App_start
This folder contains the application configuration files, like RouteConfig.cs, BundleConfig.cs, and FilterConfig.cs etc.
App_Data
This folder contains the database files, like .mdf and XML files.
Content
This folder contains CSS (Cascading Style Sheet), bootstrap, images, and icon files.
Controller
This folder contains all Controller class files.
Fonts
This folder contains all font-related files.
Models
This folder contains the database logic related classes.
Scripts
This folder contains client-side script files. Both user-defined and pre-defined JavaScript files exist in this folder.
Views
This folder contains the View pages having the extension .cshtml.
Now, let us create our first application in MVC. Open Visual Studio and create a project.
Rename your project as MvcPartOne.
Select 'MVC' in the Template section and click 'OK'. The project is created. Let us check the folder structure. The folders, such as content, script, app_start, View, Model, Controller etc. are created.
Now, right-click on the Controller folder and add a new Controller. Then, rename it as WelcomeController.
Now, add an action method and right-click on the method name, and add View.
- public ActionResult Index()
- {
-
- return View();
- }
Now, add the following line to the index method.
- ViewBag.msg = "Asp.net Mvc Part One";
Add this line in View.
Now, run the project and check the results.
Summary
In this article, we learned the basics of ASP.NET MVC, features of ASP.NET MVC and the folder structure of an MVC project. This is the first part of this article series. In this the next article, we will discuss data sharing between Controller and View and the action methods with a demo project.