MVC is an abbreviation for Model, View, and Controller. The MVC architectural pattern separates an application into three components – Model, View, and Controller. In this pattern, the model represents a shape of the data and business logic. It maintains and preserves the data of the application. Model objects retrieve and store model state in a database. The view is basically and technically a user interface. The view segment displays the data-using model to the user and also enables them to modify the data. The controller is the part, which handles the user request.
Typically, a user interacts with View, which in turn raises appropriate URL requests. Finally, a controller will handle this request. The controller segment of the MVC renders the appropriate view with the model data as a response. Implementing this structure in applications and creating web apps based on this ASP.NET framework in Azure and other platforms is what most developers ought to know.
However, one might forget the basics when dealing with practical problems on a regular basis. So, here is a list of common interview questions on the basics of MVC, which might be asked in an interview for any post or designation.
Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts, to separate the internal representation of information from the way that information is presented to or accepted by the user.
ASP.NET MVC has always supported the concept of “view engines” – which are the pluggable modules, which practically implement different template syntax options. The “default” view engine for ASP.NET MVC uses the same .aspx/.ascx/. master file templates as ASP.NET Web Forms. Other popular ASP.NET MVC view engines are Spart&Nhaml. Razor is the new view engine introduced by MVC 3.
In ASP.NET MVC 5.0 we have a new attribute route,by using the “Route” attribute we can define the URL structure. For example, in the below code we have decorated the “GotoAbout” action with the route attribute. The route attribute says that the “GotoAbout” can be invoked using the URL structure “Users/about.”
TempData is a dictionary object to store data temporarily. It is a TempDataDictionary class type and instance property of the Controller base class.
TempData can keep data for the duration of an HTTP request; in other words, it can keep live data between two consecutive HTTP requests. It will help us to pass the state between action methods. TempData only works with the current and subsequent request. TempData uses a session variable to store the data. TempData Requires type casting when used to retrieve data.
A partial view is a chunk of HTML that can be safely inserted into an existing DOM. Most commonly, partial views are used to componentize Razor views and make them easier to build and update. Partial views can also be returned directly from controller methods. In this case, the browser still receives text/HTML content but not necessarily HTML content that makes up an entire page. As a result, if a URL that returns a partial view is directly invoked from the address bar of a browser, an incomplete page may be displayed. This may be something like a page that misses title, script and style sheets.
In ASP.NET MVC there are three ways to pass/store data between the controllers and views.
ViewData
ViewBag
A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. A handler can also be a class that processes the request, such as a controller in an MVC application. To define a route, you create an instance of the route class by specifying the URL pattern, the handler, and optionally a name for the route.
Routing is a mechanism to process the incoming URL that is more descriptive and gives the desired response. In this case, URL is not mapped to specific files or folder, as was the case of websites in the past.
Routing is the URL pattern that is mapped together to a handler and is responsible for incoming browser request for a particular MVC controller. There are a total of three segments for routing that are very significant in MVC
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. For example, in one of our previous examples, the UserController class contained methods UserAdd, UserDelete, etc. However, the users would often like to perform some action before or after a particular operation.
Action Filters are additional attributes that can be applied to either a controller section or the entire controller to modify the way in which action is executed. These attributes are special .NET classes derived from system attributes, which can be attached to classes, methods, properties, and fields.
ASP.NET MVC structure upholds the following action filters:
There are total nine return types we can use to return results from a controller to view.The base type of all these result types is ActionResult.
Any web application has two main execution steps, first understanding the request and depending on the type of the request sending out an appropriate response. MVC application life cycle is not different it has two main phases, first creating the request object and second sending our response to the browser.
Creating the request object includes four basic steps:
Step 1: Fill route
Step 2: Fetch route
Step 3: Request context created
Step 4: Controller instance created
Benefits or advantages of MVC are as follows:
The MVC model defines web applications with 3 logic layers:
The Model is the part of the application, which only handles the logic for the application data. Regularly, the model objects retrieve data (as well as store data) from a database. The View is the part of the application, which takes care of the display of the data.
Most often, the views are created from the model data, although there are other, more complicated methods of creating views.
The Controller, as the name implies, is the part of the application that handles user interaction.
Default Route: The default ASP.NET MVC project templates add a generic route that uses the following URL convention to break the URL for a given request into three named segments.
URL: "{controller}/{action}/{id}"
This route pattern is registered via a call to the MapRoute() extension method of RouteCollection.
With MVC, HTML helpers are much like traditional ASP.NET Web Form controls. Just like web form controls in ASP.NET, HTML helpers are used to modifying HTML. But HTML helpers are more lightweight. Unlike Web Form controls, an HTML helper does not have an event model and a view state. HTML Helpers are mostly methods that return string values. It is not mandatory to use HTML Helper classes for building an ASP.NET MVC application as users can build an ASP.NET MVC application without using them. But HTML Helpers do help in the rapid development of a view.
As the name implies, attribute routing uses attributes to define routes. In ASP.NET MVC 5.0 we have a new attribute route. By using the “Route” attribute we can define the URL structure. For instance, in any code, a user can decorate the “GotoAbout” action with the route attribute. The route attribute says that the “GotoAbout” can be invoked using the URL structure “Users/about”. Attribute routing gives the user more control over the URLs in their web application.
Following are the rules for main Razor Syntax:
Authentication is giving access to the user for a specific service by verifying his/her identity using his/her credentials like username and password or email and password. It assures that the correct user is authenticated or logged in for a specific service and the right service has been provided to the specific user based on their role that is nothing but authorization.
Benefits of Area in MVC
A display mode is a powerful tool for all mobile-based apps development. However, it is not limited to mobile web apps but it can also be used to display any alternative view, which is tied to an existing controller. Display Modes practically give you another level of flexibility on top of the default capabilities we saw in the last section. Display Modes can also be used along with the previous feature so the users will simply build off of the site they just created. It simplifies the ways to implement different versions of view for different devices.
ASP.NET Scaffolding is a code generation framework for ASP.NET Web applications. Visual Studio 2013 includes pre-installed code generators for MVC and Web API projects. You add scaffolding to your project when you want to quickly add code that interacts with data models. Using scaffolding can reduce the amount of time to develop standard data operations in your project.
Scaffolding consists of page templates, entity page templates, field page templates, and filter templates. These templates are called Scaffold templates and allow you to quickly build a functional data-driven Website.
Routing is a great feature of MVC because it provides a REST-based URL which is very easy to remember. It also improves page ranking in search engines.
This article is not an introduction to Routing in MVC, but we will learn a few features of routing, and by implementing them we can develop a very flexible and user-friendly application. So, let’s start without wasting valuable time.
The main objective of making use of Output Caching is to dramatically improve the performance of an ASP.NET MVC Application. It enables us to cache the content returned by any controller method so that the same content does not need to be generated each time the same controller method is invoked. Output Caching has huge advantages, such as it reduces server round trips, reduces database server round trips, reduces network traffic, etc.
Bundling and minification are two new techniques introduced to improve request load time. It improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript).
Bundling: It lets us combine multiple JavaScript (.js) files or multiple cascading style sheet (.css) files so that they can be downloaded as a unit, rather than making individual HTTP requests.
Minification: It extracts the whitespace and performs other types of compression to make the downloaded files as small as possible. At runtime, the process recognizes the agent of the user, for example, IE, Mozilla, etc. and then removes whatever is specific to Mozilla when the request comes from IE.
The Validation Summary helper method generates an unordered list (UL element) of validation messages that are in the Model State Dictionary object.
The Validation Summary can be used to display all the error messages for all the fields. It can also be used to display custom error messages. The following figure shows how Validation Summary displays the error messages.
Database First Approach is an alternative or substitutes to the Code First and Model First approaches to the Entity Data Model. The Entity Data Model creates model codes (classes, properties, DbContext, etc.) from the database in the project and that class behaves as the link between database and controller.
There are the following approaches, which are used to connect the database with the application.
GET Action Type: GET is used to request data from a specified resource. With all the GET requests, we pass the URL, which is compulsory; however, it can take up the following overloads.
POST Action Type: Tthe POST is used to submit data to be processed to a specified resource. With all the POST requests, we pass the URL, which is essential and the data. However, it can take up the following overloads.
In MVC 6 Microsoft removed the dependency of System.Web.Dll from MVC6 because it is extremely expensive, as it typically used to consume 30K of memory per request and response, whereas now MVC 6 only requires 2K of memory per request and the response is really small memory consumption.
Never Miss an Articles from us.