Nagi Nabal

 

ASP.NET MVC 5 Framework - Part 1

Introduction

In this blog, we will be learning about what is ASP.NET MVC Framework tool, Model-view-Controller (MVC) pattern and the advantages of using it. Also, we will be going through how to create a new project that will using this tool to help us build a powerful web application quickly and easily.

 

What is ASP.NET MVC 5 Framework?

ASP.NET MVC is the tool of choice for building modern, dynamic, and scalable web application. It provides an extensible, high-quality programming model to build dynamic, data-driven website, focusing on a cleaner architecture and test-driven development. By learning what model, views, and controllers are and how is passed between them it helps you create and perform unit tests to ensure you build software that works the way it is supposed to.


Model-View-Controller (MVC) pattern

The model view controller pattern is a very useful design pattern that ASP.NET MVC follows and was named for. The first and central part of MVC patter is the model. Models are objects that represent the data or the internet state of the application. They are also meant to contains most of the application’s logic. Views are the part of the MVC pattern that the user sees and interacts with. The view receives user interaction and input and passes those on to the model. The Controller is responsible for the traffic in an MVC application. It handles a request and routes it to the correct view and model. In the ASP.NET MVC application, your controller code is usually the first code to execute for each web request. 

Looking at the diagram above you can see how these components, the mode, view, and controller, fit together. Starting at the bottom of this diagram where the user triggers a controller, you can see that the controller creates, loads, or otherwise manipulates a model object to represent the application state changes that the user requested. Then, the model class is given to the view where it is bound to the view or otherwise changes the view causing it to be updated. Finally, the view is presented to the user where they can choose to interact with the application again, restarting this cycle.


How MVC services requests

Before getting into creating an ASP.NET MVC project, it will be helpful to understand how it handles incoming HTTP request and creates the outgoing HTTP responses. The ASP.NET MVC 5 framework follows a very specific pattern for this request and response cycle, allowing you multiple different places along that path to intervene with your own code, but only one place where you have to. 


As we can see from the dialog above, each web request starts with a use making an HTTP request. ASP.NET uses routing tables, which are defined at start up. These routes are used to figure out which controller class to route the request to. Once the controller instance has been created, the appropriate action methos in that class can be executed. Now your program code us being run, specifically, your code in the controller class. The result of the action method is an object that inherits from the action result-based class. This object is what is executed on the next step. Sometimes, the action result represents a simple HTTTP response or JSON formatted string. But in MVC applications, it uses built-in view engine called Razor. If the result is a Razor view, the view engine is used during the result execution to turn that Razor into HTML that can be displayed by the browser. The HTTP response is sent back to the user’s browser where it downloads and displays the page to the user to interact with. 


Tools used to implement MVC framework

Throughout this blog we will be building a real ASP.NET MVC 5 application. To do that, we will be using several very common tools that .NET developers use to build lots of .NET project, including ASP.NET MVC 5 project. Probably the most common .NET development tool, and the primary one we will be using is Visual Studio 2019. The next one is SQL Server we need to use but you do not need to install the full version of SQL on your computer. Visual Studio comes with a development version of SQL Server Called SQL Express LocalDB that you can use. The following video I will show how to make sure you have SQL Express LocalDB installed after you installed the Visual Studio application. 

 



Create a new project using Visual Studio

The following video will show you how to create a new MVC 5 project using Microsoft Visual Studio 2019 community edition step-by-step:




Walking through the new project template

The following video will show you what are the template files that created by the project and what the purpose for each one of them: 




Advantages of using MVC

  • Faster development process.  
  • Make your application easier to maintain and create.
  • Help you organize large-size web applications.
  • Allows easy modification of the entire application.
  • MVC model returns the data without formatting which means the MVC framework empowers you to create your own view engine.
  • Ability to provide multiple views which means developing different view components for your model component is easily achievable.

 

         👉 Check out Part-2 here: ASP.NET MVC 5 - Part 2

Comments

Popular posts from this blog