Nagi Nabal


 ASP.NET MVC 5 Framework - Part 2


Introduction                                                                                                                                           

In this blog, we will continue learning about the ASP.NET MVC Framework tool and goes deep to discuss and create each of the MVC pattern Models, Views, and Controller. Also, how to use Filters to customize your action and secure your app with it. Finally, we will continue working on the same project from the last video in Part-1 and show you how to create and use each of the MVC patterns.

👉 Check the first part here: ASP.NET MVC 5 - Part 1 


Controller                                                                                                                      

MVC controllers are responsible for responding to requests made against an ASP.NET website. Each browser request is mapped to a particular controller. For example, imagine that you enter the following URL into the address bar of your browser:


In this case, the figure below shows the index() method is called on the HelloController class. The index() method is an example of a controller action.

A controller action must be a public method of a controller class. The language we use in this project is C#. By default, C# methods are private methods. Realize that any public method that you add to a controller class is exposed as a controller action automatically. 


What is Action Result that we use in the previous example?

A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request.

The ASP.NET MVC framework supports several types of action results including

  • ViewResult – Represents HTML and markup
  • EmptyResult – Represents no result
  • RedirectResult – Represents a redirection to a new URL
  • JavaScriptResult – Represents a JavaScript script
  • ContentResult – Represents a text result
  • FileContentResult – Represents a downloadable file (with binary content)
  • FilePathResult – Represents a downloadable file (with a path)
  • FileStreamResult – Represents a downloadable file (with a file stream)
  • JsonResult – Represents a JavaScript Objects Notation result that can be used in an AJAX application

All of these action results inherit from the base ActionResult class.

The video below will show you how to create a controller with a simple view response.


Views                                                                                                                              

In an ASP.NET MVC application, there is nothing like a page and it does not include anything that directory corresponds to a page when you specify a path in the URL. The closest thing to a page in an ASP.NET MVC application is known as a View.
In the ASP.NET MVC application, all incoming browser requests are handled by the controller and these requests are mapped to controller actions. A controller action might return a view, or it might also perform some other type of action such as redirecting to another controller action.
In the MVC 5, the routing table follows certain naming conventions. These conventions can be reconfigured, but I find that it is usually an excellent default way to organize your code. With this default convention MVC 5 will search for a view in a couple of different locations:
  • First, it will look for either a .cshtml or a .vbhtml file in a folder with the same name as the controller under the Views folder. Both .cshtml and .vbhtml files are a format called Razon views. One writer in C sharp (C#) and the other in VB.NET.
  • Second, it will search for a view in the Shared folder, underneath the Views folder. Using just the action name omitting anything about the controller’s name.
See the figure below for more information about Views naming conventions. 


Passing Data from Controller to View

Once a request is routed to the correct controller and that controller retrieves the correct data, that data must be displayed on the view. MVC 5 provides several different ways to do this:
  • ViewData – it is a dictionary of objects, each keyed by a string that you can set in the controller and then retrieve using the same string in the view code.
  • ViewBag – it is a natural evolution of the ViewData dictionary. Instead of being a dictionary of objects though, it is a dynamic type. So you can write code to set or get a property of the ViewBag object without having to ever define that property ahead of time.
  • TempData – it works exactly like the ViewData, but it is valued last for the current request and the next request. Unlike ViewData and ViewBag, which only last for the current request cycle.
  • Custom model class – ASP.NET MVC provides the ability to pass custom model classes to your view.


Filters                                                                                                                             

Sometimes, you want to execute the same code in multiple, different, action methods. In some cases, you could accomplish this by using object-oriented programming practices, such as Inheritance, but ASP.NET MVC provides another simple way to accomplish it using something called Filters. Filters allow you to inject some extra logic into the request processing pipeline of an MVC 5 application.

Types of Filters in ASP.NET MVC 5:

  • Authentication – responsible for verifying your identity via a cookie.
  • Authorization – responsible to secure access to only logged-in users accessing the account page.
  • Action – these run before and after the action methods.
  • Result – runs before and after the action result are executed.
  • Exception – runs only if another filter, the action method, or the action result throws an exception.

The video below will show you how to secure your application using Filters:



Models                                                                                                                           

An MVC model contains all of the application's logic that is not contained in a View or a Controller. The model should contain all of your application business logic, validation logic, and database access logic.

A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action. Everything else should be contained in the model.

Using validation with data notations

Consistent and easy data validation is one of the most important things a framework can provide. There are several built-in attributes that will cover most validation situations, but if not, you can always create a custom validation attribute yourself. The figure below shows the built-in validation attribute in ASP.NET MVC 5: 



The video below will show you how to create a validation attribute with data annotations and use it with ModelState to display it in the View:


In conclusion, we learn through parts one and two what is ASP.NET MVC 5 and how to use this framework to build a web application based on Model-View-Controller (MVC) architecture that enables a clean separation of concerns, fast development, and TDD friendly.

I hope you enjoy reading my blogs. Let me know if you have any questions or concerns. 


Comments

Popular posts from this blog