Latest MVC Interview Question & Answers


MVC interview questions

1. What is Output Caching in MVC?

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.
2. Explain the need of display mode 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.

3. What is Default Route in MVC?

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. 
4. Explain what is MVC application life cycle?
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 

5. What are Action Filters in MVC?

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.
6.  Explain what is routing in MVC?
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.
7. What is Partial View in MVC?
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.
8. What is TempData in MVC?
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.
9. What is Razor in MVC?
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.
10. Explain what is Database First Approach in MVC using Entity Framework?
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.
  • Database First
  • Model First
  • Code First
11. What is Bundling and Minification in MVC?
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.
12. Explain the concept of MVC Scaffolding?
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.
13. How to render html in Asp.net MVC view?

14. What’s new in the latest version of MVC?
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.
15.  What is Validation Summary in MVC?
 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.
16. What is GET and POST Actions Types?
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. 
17. What is MVC (Model view controller)?
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.
18. Explain attribute based routing in MVC?
 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.”
19.  Mention what is the difference between ViewData and ViewBag?
 In ASP.NET MVC there are three ways to pass/store data between the controllers and views.
ViewData
  1. ViewData is used to pass data from a controller to view.
  2. It is derived from ViewDataDictionary class.
  3. It is available for the current request only.
  4. Requires typecasting for complex data type and checks for null values to avoid error.
  5. If redirection occurs, then its value becomes null.
ViewBag
  1. ViewBag is also used to pass data from the controller to the respective view.
  2. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0
  3. It is also available for the current request only.
  4. If redirection occurs, then its value becomes null.
  5. Doesn’t require typecasting for the complex data type.
20. What is Route in MVC?
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.
21. What are the three segments for routing important?
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
  1. ControllerName
  2. ActionMethodName
  3. Parammeter
22. What are the Filters 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. 
23.  What are the types of filters?
ASP.NET MVC structure upholds the following action filters:
  • Action Filters: Action filters are used to implement logic that gets executed before and after a controller action executes. We will look at Action Filters in detail in this chapter.
  • Authorization Filters: Authorization filters are used to implement authentication and authorization for controller actions.
  • Result Filters: Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.
  • Exception Filters: 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.
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.
  1. ViewResult (View): This return type is used to return a webpage from an action method.
  2. PartialviewResult (Partialview): This return type is used to send a part of a view which will be rendered in another view.
  3. RedirectResult (Redirect): This return type is used to redirect to any other controller and action method depending on the URL.
  4. RedirectToRouteResult (RedirectToAction, RedirectToRoute): This return type is used when we want to redirect to any other action method.
  5. ContentResult (Content): This return type is used to return HTTP content type like text/plain as the result of the action.
  6. jsonResult (JSON): This return type is used when we want to return a JSON message.
  7. javascriptResult (javascript): This return type is used to return JavaScript code that will run in the browser.
  8. FileResult (File): This return type is used to send binary output in response.
  9. EmptyResult: This return type is used to return nothing (void) in the result.
25.  What are the advantages of MVC?
Benefits or advantages of MVC are as follows:
  • Multiple view support: Due to the separation of the model from the view, the user interface can display multiple views of the same data at the same time.
  • Change Accommodation: User interfaces tend to change more frequently than business rules (different colors, fonts, screen layouts, and levels of support for new devices such as cell phones or PDAs)
  • SoC – Separation of Concerns: Separation of Concerns is one of the core advantages of ASP.NET MVC. The MVC framework provides a clean separation of the UI, Business Logic, Model or Data.
  •  More Control: The ASP.NET MVC framework provides more control over HTML, JavaScript, and CSS than the traditional Web Forms.
  • Testability: ASP.NET MVC framework provides better testability of the Web Application and good support for the test-driven development too.
  •   Lightweight: ASP.NET MVC framework doesn’t use View State and thus reduces the bandwidth of the requests to an extent.
26. What does the MVC pattern define with 3 logical layers?
The MVC model defines web applications with 3 logic layers:
  • The business layer (Model logic)
  • The display layer (View logic)
  • The input control (Controller logic)
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.
27. What are HTML helpers in MVC?
 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.
28. Define attribute based routing in MVC?
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.
29.  What are the Main Razor Syntax Rules?
Following are the rules for main Razor Syntax:
  • Razor code blocks are enclosed in @{ … }
  • Inline expressions (variables and functions) start with @
  • Code statements end with a semicolon
  • Variables are declared with the var keyword
  • Strings are enclosed with quotation marks
  • C# code is case sensitive
  • C# files have the extension .cshtml
30. How do you implement Forms authentication in MVC
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.
31. What are the Benefits of Area in MVC?
Benefits of Area in MVC
  1. Allows us to organize models, views, and controllers into separate functional sections of the application, such as administration, billing, customer support and much more.
  2. Easy to integrate with other Areas created by another.
  3. Easy for unit testing.
32.  What are Route Constraints in MVC?
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.

No comments:

Post a Comment