MVC Behind the Scenes: Everything Developers Should Know
What do you understand about Model View Control?
MVC is a software architecture or application design model containing 3 interconnected verticals or portions. These 3 portions are the model (data associated with the application), the view (which is the user interface of an MVC application), and the controller (the processes that are responsible for handling the input).
The MVC model is normally used to develop modern applications with user interfaces. It provides the central pieces for designing a desktop or mobile application, as well as modern web applications.
What do you understand about Model View Control?
MVC is a software architecture or application design model containing 3 interconnected verticals or portions. These 3 portions are the model (data associated with the application), the view (which is the user interface of an MVC application), and the controller (the processes that are responsible for handling the input).
The MVC model is normally used to develop modern applications with user interfaces. It provides the central pieces for designing a desktop or mobile application, as well as modern web applications.
Explain the Model, View and Controller in brief.
The MVC model defines web applications with 3 logic layers,
- The business layer (Model logic): The Model is the part of the application that handles the logic for the application data. Often model objects retrieve data (and store data) from a database.
- The display layer (View logic): The View is part of the application that handles the display of the data. Most often the views are created from the model data.
- The input control (Controller logic): The Controller is the part of the application that handles user interaction. Typically controllers read data from a view, control user input, and send input data to the model.
The MVC separation helps you manage complex applications because you can focus on one aspect at a time. For example, you can focus on the view without depending on the business logic. It also makes it easier to test an application.
What are the different return types used by the controller action method in MVC?
The various return types of controller action methods in MVC are:
- ViewResult: Derived from the “ViewResultBase” class. “ViewResultBase” is derived from Action Result. It returns the view page (HTML page, view page has “.cshtm”)
public ViewResult AboutUs() { ViewBag.Message ="Welcome guest"; return View(); }
- PartialviewResult (Partial view): This return type is used to send a part of a view that will be rendered in another view.
- JSONResult: Action methods on controllers return JsonResult (JavaScript Object Notation result) that can be used in an AJAX application. This class is inherited from the "ActionResult" abstract class. Here, JSON provides one argument that must be serializable. The JSON result object that serializes the specified object to JSON format.
- ContentResult: The content result returns different content formats to view.
- RedirectResult: This return type is used to redirect to any other controller and action method depending on the URL.
- RedirectToRouteResult: This return type is used when we want to redirect to any other action method.
- JavaScriptResult: This return type is used to return JavaScript code that will run in the browser.
- ActionResult: Redirect to Action result is returning the result to a specified controller and action method.
- EmptyResult: This return type is used to return nothing (void) in the result.
- FileResult: File Result returns a different file format view page when we implement the file download concept in MVC using file result.
Explain the MVC Application Life Cycle
Web applications usually have two primary execution steps:
- Understanding the request
- Sending an appropriate response based on the type of request
The same applies to MVC applications, which have two foremost phases:
- Creating a request object
- Sending the response to any browser
Different Steps or Stages of the MVC Page Life Cycle
- Initialization of app
- Routing
- Instantiate the object followed by executing the controller
- Locate as well as invoke the controller action
- Instantiating and then rendering the view
Creating the Request Object
The request object creation has four major steps:
- Step 1 - Fill route: MVC requests are mapped to route tables that specify which controller and action to invoke. If the request is the first one, the routing table is filled with a routes collection in the
global.asax
file. - Step 2 - Fetch route: Based on the URL sent, the
UrlRoutingModule
searches the routing table to create theRouteData
object, which contains details of which controller and action to invoke. - Step 3 - Request context created: The
RouteData
object is used to create theRequestContext
object. - Step 4 - Controller instance created: The request object is sent to the
MvcHandler
instance to create the controller class instance. Once the controller object is created, it calls theExecute
method of the controller class.
What are the benefits of Using MVC?
- Support for multiple views: Since the model is separate from its view, the UI can implement multiple views of the same data concurrently.
- Faster development process: MVC enables rapid and parallel development, allowing one developer to work on the view while another focuses on business logic.
- SEO-friendly development: The MVC framework supports SEO-friendly development of web pages or applications.
- More control: The ASP.NET MVC framework offers greater control over HTML, CSS, and JavaScript compared to traditional WebForms.
- Lightweight: MVC does not use ViewState, reducing the requested bandwidth.
Explain in brief the role of different MVC components
- Presentation: Handles the visual representation of an abstraction within the application.
- Control: Maintains consistency and uniformity among abstractions, manages the presentation, and communicates with other controls within the MVC system.
- Abstraction: Manages business domain functionality within the application.
How will you maintain the sessions in MVC?
Sessions in MVC can be maintained in three possible ways:
- ViewData
- TempData
- ViewBag
Difference Between TempData, ViewData, and ViewBag?
Temp Data | View Data | View Bag |
---|---|---|
TempData is a dictionary object derived from the TempDataDictionary class |
It is derived from the ViewDataDictionary class. |
|
TempData is used to pass data from the current request to the next request, in other words in the case of redirection. |
ViewData is used to pass data from the controller to view. |
ViewBag is also used to pass data from the controller to the respective view. |
It keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action. TempData can preserve data for an HTTP request. You can retain its value using the Keep method for subsequent requests. |
It is available for the current request only. If redirection occurs, then its value becomes null. |
It is also available for the current request only. If redirection occurs, then its value becomes null. |
It requires typecasting for complex data types and checks for null values to avoid an error. Generally, it is used to store only one-time messages like error messages and validation messages |
Requires typecasting for complex data types and checks for null values to avoid an error. Calling of ViewDatais : ViewData["Name"] = "Yogesh"; |
ViewBag does not require typecasting for complex data types so you can directly access the data from ViewBag. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0 |
How will you define the 3 logical layers of MVC?
The 3 logical layers of MVC can be defined as follows:
- Model logicacts as a business layer
- View logic acts as a display layer
- Controller logicacts as input control
Define the concept of Filters in MVC.
Controllers define action methods and these action methods generally have a one-to-one relationship with UI controls such as clicking a button or a link, etc.
But many times we would like to perform some action before or after a particular operation. To achieve this functionality, ASP.NET MVC provides a feature to add pre and post-action behaviours on the controller's action methods.
Types of Filters
- Action Filters (Implements IActionFilter): Action filters are used to implement logic that gets executed before and after a controller action executes.
- Authorization Filters(Implements IAuthorizationFilter): Authorization filters are used to implement authentication and authorization for controller actions.
- Result Filters (Implements IResultFilter): Result filters contain logic that is executed before and after a view, and the result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.
- Exception Filters(Implements IExceptionFilter): Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results. You can also use exception filters to log errors.
Action filters are one of the most commonly used filters to perform additional data processing, manipulating the return values, cancelling the execution of an action or modifying the view structure at run time.
Explain briefly the 2 approaches of adding constraints to an MVC route.
For adding constraints to an MVC route, the 2 different approaches are:
- By making use of regular expressions
- By making use of objects that implement the “IRouteConstraint” interface.
here are two types of routing (after the introduction of ASP.NET MVC 5).
- Convention-based routing - to define this type of routing, we call the MapRoute method to set its unique name, and URL pattern and specify some default values.
- Attribute-based routing - to define this type of routing, we specify the Route attribute in the action method of the controller. In the below example, it can be invoked using the URL structure "Users/about".