Nagi Nabal
ASP.NET MVC 5 Framework - Part 2
Introduction
Controller
What is Action Result that we use in the previous example?
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 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.
Passing Data from Controller to View
- 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
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
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
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:
I hope you enjoy reading my blogs. Let me know if you have any questions or concerns.
Comments
Post a Comment