Interview questions for MVC

1. What is the use of View Model in MVC?

View Model is a plain class with properties, which is used to bind it to strongly typed view. ViewModel can have the validation rules defined for its properties using data annotations.

2. Explain the MVC application life cycle.

Any web application has two primary execution steps:
• Understanding the request
• Sending out an appropriate response
MVC application life cycle includes two main phases:
• Creating the request object
• Sending response to the browser
The four steps included in creating a request object are:
• filling route
• fetching route
• requesting context created
• controlling instance created

3. What you mean by Routing in MVC?

Routing is a pattern matching mechanism of incoming requests to the URL patterns which are registered in route table. Class — “UrlRoutingModule” is used for the same process.
11) What are Actions in MVC?
Actions are the methods in Controller class which is responsible for returning the view or json data. Action will mainly have return type — “ActionResult” and it will be invoked from method — “InvokeAction()” called by controller.
2. What do you understand by filters in MVC?
Controllers in MVC define action methods, and these methods have a one-to-one relationship with UI controls. Example: UserController class contains UserAdd and UserDelete methods. However, many times, we wish to perform some action after or before a particular operation. With the feature provided by ASP.NET MVC, pre and post-action behaviors can be added to the controller’s action methods.

12) What is Attribute Routing in MVC?
ASP.NET Web API supports this type routing. This is introduced in MVC5. In this type of routing, attributes are being used to define the routes. This type of routing gives more control over classic URI Routing. Attribute Routing can be defined at controller level or at Action level like –
[Route(“{action = TestCategoryList}”)] — Controller Level
[Route(“customers/{TestCategoryId:int:min(10)}”)] — Action Level
7. How is routing carried out in the MVC pattern?
A group of routes known as RouteCollection consists of registered routes in the application. The RegisterRoutes method records the collection routes. The route and a handler define the URL pattern if the request matches the pattern. The route’s name is the first parameter to the MapRoute; the second parameter is the pattern to which URL matches, and the third parameter is default values for the placeholders.
8. What is the difference between ‘ViewResult’ and ‘ActionResult’?
ViewResult is derived from the ‘AbstractResult’ class, and ‘ActionResult’ is an abstract class. ActionResult is good when you are dynamically deriving different types of views. The derived classes of ActionResult are FileStreamResult, ViewResult, and JsonResult.
9. Give the importance of NonActionAttribute.
If we want to prevent the default nature of a public method of a controller from being treated as an action method, then we assign the NonActionattribute to the public method.

13) How to enable Attribute Routing?
Just add the method — “MapMvcAttributeRoutes()” to enable attribute routing as shown below
public static void RegistearRoutes(RouteCollection routes)
{
routes.IgnoareRoute(“{resource}.axd/{*pathInfo}”);
//enabling attribute routing
routes.MapMvcAttributeRoutes();
//convention-based routing
routes.MapRoute
(
name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Customer”, action = “GetCustomerList”, id = UrlParameter.Optional }

14) Explain JSON Binding?
JavaScript Object Notation (JSON) binding support started from MVC3 onwards via the new JsonValueProviderFactory, which allows the action methods to accept and model-bind data in JSON format. This is useful in Ajax scenarios like client templates and data binding that need to post data back to the server.
7. What do you understand by WebAPI?
WebAPI is a technology with which you can expose data over HTTP using the REST principles. This approach was proposed to satisfy a broad range of clients requiring to consume data from JavaScript, mobile, Windows, etc.
8. Tell us the benefit of using an IoC container in an application.
The benefits include external management of life of every object, change of implementation of the contract in future, change to dependency list not affecting an object using the service, and sharing of an instance by several unrelated customers.
9. Can you tell two instances where routing is not required or implemented?
We do not require routing when routing is disabled for a URL pattern and when a physical file matching the URL pattern is found.
9. Tell us the benefit of the area in MVC.
The Area in MVC helps to integrate with other areas generated by other apps, helps organize views, controllers, and models in different functional sets, and is suitable for unit testing.
10. What do you understand by Model Binding?
In action methods, we need to retrieve data from requests to be used by the data. In MVC, model binding maps data from HTTP request to action method parameters. This task of retrieving data from HTTPRequest is repetitive and is removed from the action method.

15) Explain Dependency Resolution?
Dependency Resolver again has been introduced in MVC3 and it is greatly simplified the use of dependency injection in your applications. This turn to be easier and useful for decoupling the application components and making them easier to test and more configurable.
16) Explain Bundle.Config in MVC4?
“BundleConfig.cs” in MVC4 is used to register the bundles by the bundling and minification system. Many bundles are added by default including jQuery libraries like — jquery.validate, Modernizr, and default CSS references.
17) How route table has been created in ASP.NET MVC?
Method — “RegisterRoutes()” is used for registering the routes which will be added in “Application_Start()” method of global.asax file, which is fired when the application is loaded or started.
18) Which are the important namespaces used in MVC?
Below are the important namespaces used in MVC –
System.Web.Mvc
System.Web.Mvc.Ajax
System.Web.Mvc.Html
System.Web.Mvc.Async
19) What is ViewData?
Viewdata contains the key, value pairs as dictionary and this is derived from class — “ViewDataDictionary“. In action method we are setting the value for viewdata and in view the value will be fetched by typecasting.
20) What is the difference between ViewBag and ViewData in MVC?
ViewBag is a wrapper around ViewData, which allows to create dynamic properties. Advantage of viewbag over viewdata will be –
In ViewBag no need to typecast the objects as in ViewData.
ViewBag will take advantage of dynamic keyword which is introduced in version 4.0. But before using ViewBag we have to keep in mind that ViewBag is slower than ViewData.

leave your comment


Your email address will not be published. Required fields are marked *