MVC interview questions For Freshers & Experienced

  • Question 1) What is MVC? and Full form,
  • Question 2) Explain the mvc application life cycle?
  • Question 3) What is MvcHandler in ASP.NET?
  • Question 4) Please Explain Display Mode in MVC?
  • Question 5) What is Output Caching in MVC?
  • Question 6) Enlist Different Types of Caching in asp.net MVC?
  • Question 7) What are Action Filters in MVC?
  • Question 8) What is default route in mvc?
  • Question 9) What is Razor in MVC?
  • Question 10) List different types of filter available in MVC?
  • Question 11) What are HTML helpers in MVC?
  • Question 12) What is Attribute Routing in MVC?
  • Question 13) Enlist some important namespaces used in Asp.net MVC?
  • Sharad Jaiswal
  • 28th Dec, 2019
  • 651 Followers

MVC interview questions
Updated ON

28th Dec, 2019

No. of Questions

20

Posted In

Microsoft

Author

Sharad Jaiswal

What is Asp.net MVC?

ASP.NET MVC is an open-source framework for web development that offers a Model-View-Controller architecture from Microsoft. It is an alternative for web forms in ASP.NET and is an integral part of the .NET platform for deploying, building, and running web applications. ASP.NET MVC excels in issues for response time, Unit Testing, customization of HTML, and reusability of the code class.

It does not support view state and includes route-based URLs that are redirected to actions and controllers. ASP.NET MVC follows syntax that can be customized and includes layouts for a consistent look. It offers a partial view of the reusability of the code.

MVC interview questions

Q1. What is MVC? and Full form,

MVC Stands For Model View Controller:- 

It is a design model that comprises the Model(data), view (the user interface), and the controller (handles the input).

Q2. Explain the mvc application life cycle?

In the MVC lifecycle, first, the request to the MVC application is sent from the client browser. The IIS determines that the request needs to be processed by the ASP.net application. The requested URL is first routed to the MVC application by the MvcRouteHandler. After intercepting the request, the HttpContextWrapper object is created. Then, the MVC handler executes the BeginProcessRequest method call. When the method is called, an MVC controller is created from a ControllerFactory.

After creating the controller, the MVC handler invokes the execute method on the controller. Then the controller executes the necessary method based on the request. ViewResult locates the corresponding view and invokes the render method. After rendering the result view, it is displayed to the client browser.

Q3. What is MvcHandler in ASP.NET?

The handler in the MVC is responsible for handling the request from the user browser and producing the appropriate response for the specific content. The MVC handler class doesn’t produce the response by itself.

It uses the ControllerBuilder object for creating a Controller from the ControllerFactory and calls the Execute method in the controller. The Execute method has the responsibility to find and call the appropriate action for the request. Then the action is wrapped to return it in the HTTP response as a result by the handler class.

Q4. Please Explain Display Mode in MVC?

The Display Mode in the ASP.net is used to render the page content based on the requesting device. The requesting device can be web, mobile, tablet, iPhone, or android, the display mode will render the page content based on the size and the variety of the device.

To do this, You Need to create a display mode for each class of devices. The display mode will check the “user-agent" headers and renders the page appropriate for the devices.

The Display Mode can be Set Up when the Application Starts that is, global.asax file.

//example for the display mode setup for android and windows phone
DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("iPhone") { ContextCondition = (ctx => ctx.GetOverriddenUserAgent() .IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) > 0) }); DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("Android") { ContextCondition = (ctx => ctx.GetOverriddenUserAgent() .IndexOf("Android", StringComparison.OrdinalIgnoreCase) > 0) });

Q5. What is Output Caching in MVC?

The output cache in the MVC is used to cache the content returned by the controller. So, every time the controller method is invoked, it doesn’t need to regenerate the content as it is saved in the output cache. Output cache improves the performance of the ASP.net MVC application significantly. This cache should be used for content that is accessed frequently and the same cache should not be used for different users.

The output cache improves the application performance by reducing the network traffic, server round trips, and the database round trips.

Q6. Enlist Different Types of Caching in asp.net MVC?

Caching provides the user with a way of storing the frequently accessed data to improve the performance of the application.

In ASP.net MVC, there are Three ways of caching content.

  1. Static content caching

    Static content is a common form of data in the web application. Static content doesn’t change dynamically, it remains the same throughout the life of the application like the CSS or HTML content. These contents are heavy to load from the server each time the application is called. So, it should be cached to improve performance. Static content caching can be configured in the Web. Config file.

  2. Caching using OutputCache Attribute

    Here, the page response or the content from the action method is cached by using the OutputCache attribute. This attribute can be used with a controller or activity level to improve the performance of the application.

  3. Shared data Caching

    The data that needs to be shared between multiple action methods can be cached using the HttpContext.Cache. It can be used to get or set the data in the cache to improve the performance.

Q7. What are Action Filters in MVC?

Action Filter in MVC is a type of filter that is executed before and after the execution of the action method. OutputCache is an example of a built-in action filter in MVC.

These filters can be applied to an individual action method or the whole controller.

//example for an action filter – OutputCache 
[OutputCache(Duration=10)] public ActionResult Index() { return View(); }

Note:- In the above example, the output is cached for 10 seconds.

MVC Gives the user an option to create a custom action filter. It can be implemented by using the IActionFilter interface and the FilterAttribute class, Or by using the ActionFilterAttribute abstract class.

Q8. What is default route in mvc?

The ASP.net MVC application has a default route table which contains the single default route. This default route maps the first segment of the URL to the controller Name. Then, the second segment of the URL to the controller action, and finally, the last segment of the URL to a parameter called id.

If the controller is not specified, then that parameter defaults to the value Home. If the controller action is not specified, then that parameter defaults to Index. If the parameter is not specified the defaults to empty string.

Q9. What is Razor in MVC?

Razor is a view engine supported by ASP.NET MVC that lets the user embed the server code into the web page using C# and VB.Net. Razor uses a server-side markup language that has the following characteristics,

  • Razor is compact that has a minimum number of characters to write a code
  • It is easy to learn if you are familiar with C# or VB.
  • It supports intellisense in Visual Studio.

Q10. List different types of filter available in MVC?

Filters are used to implement logic at various levels in the application like logging, authorization, or caching.

There are five filters available in the ASP.net MVC.

  • Authentication Filter

It runs first before another type of filter. It is used for the authentication purpose of individual users. It is implemented using the IAuthenticationFilter interface.

  • Authorization Filter

It is used to check the authorization for user access. AuthorizeAttribute is an example of authorization filter. It implements the IAuthorizationFilter interface.

  • Action Filter

It is used to call before or after an execution of action methods to modify data, etc. It is implemented from the IAction interface.

  • Result Filter

It is executed before or after a view result is executed to modify it. It implements the IResultFilter Interface. OutputCacheAttribute is an example of this type.

  • Exception Filter

These are used to handle the exceptions thrown. It implements the IExceptionFilter Interface. HandleErrorAttribute class is an example of this type.

Q11. What are HTML helpers in MVC?

The HTML helper class is used to generate the HTML controls programmatically. It displays the model properties into HTML elements by binding the model object to HTML elements. While submitting the web form, the HTML helper class assigns the value of the form of HTML elements to the model properties. It is not necessary to use an HTML helper class while building an ASP.net MVC application, but using it improves the speed in the development of a view as it is lightweight than a web form. The three main HTML helpers are,

  • Inline HTML helpers
  • Bult-in HTML helpers
  • Custom HTML helpers

Q12. What is Attribute Routing in MVC?

Routing is the process of matching a URI to its action. Attribute routing is a type of routing in MVC, that uses attributes to define the route. It gives more control over the URI in the application. To use this type of routing, call the MapMvcAttributeRoutes method. It is possible to combine the attribute routing with conventional routing.

Q13. Enlist some important namespaces used in Asp.net MVC?

A namespace can be defined as a container of Class, Interface, Structures, Delegations, Enumerations, etc. Using namespace, it is possible to use the same class name without collision.

Some of the important namespace in the ASP.NET MVC is,

  • System.Web.Mvc

It includes controllers, libraries, actions, views, and models to create web applications. It follows the MVC pattern.

  • System.Web.Mvc.Ajax

It is used in web applications. It has classes that supports AJAX scripts, and AJAX related settings and options.

  • System.Web.Mvc.Async

It has classes and interface that supports the development of asynchronous actions in web applications.

  • System.Web.Mvc.Html

It includes classes for forms, controls, links, views, and validations to support the helper application. It follows the MVC pattern.

Q14. How to bind an object from controller to view in mvc?

The binding object is a way of passing data from the controller to the view using the controller.

To pass the data,

Create your model class and controller first. Use the return statement to return the object from your model class. Then import the binding object of the model class at the top of your Index view and access the properties of the object using the @Model.

//example 
return View(object) //in model class, return the object. @model Appname.Models.ClassName //use this binding object statement at the top of your Index Var data = @Model.data //accessing the model class object properties using the @Model

Q15. What is viewbag in mvc?

ViewBag is used to send data or objects from your controller to your view. It is a dynamic type property of the ControllerBase class.

//example 
ViewBag.data = “value"; In the model class, you can store some data to the ViewBag by attaching it to a property. Var data = ViewBag.data;

Note:- In the view, you can get the data from the ViewBag by using the property.

Q16. Explain the use of ViewModel in MVC?

The view model is used to represent the data on the view/page. It is a model for the view that is used for static text or data from/to the databases that can be represented in your view. A view model is just an object that has all the data and the properties to render a view. It can also be used for validation of data using IDataErrorInfo, handling user input, retrieving data from the database, combining data, etc. Also, display templates specified in the view model can be easily reused in other places.

Q17. What is use Bundle.Config.cs file in MVC4?

Bundle.config is a file created in default by MVC that is used to write the bundling code. When the individual request is sent to the server when the user references different files, there occurs unnecessary server overload. Bundling allows the developer to create a bundle of all the javascript and CSS files in the application. With this, all the files are fetched using a single request which improves the performance. Also, the bundles in the bundle.config files are minified. That is, all the extra space, the unnecessary comment is removed and scoped variable names are shortened to minimize the file size. Using the bundle and the minified files in the bundle.config files, it makes the job for a developer easy by referencing a set of files using a single request and thereby making the code clean and readable.

Q18. What is TempData in MVC?

TempData is used to pass data between the controller and the view. It is a dictionary object from the TempDataDictionay that stores data to be used in subsequent requests. This data is cleared after the completion of the request. The data in the TempData is stored as a key/value pair.

//example
 
TempData[“name”] = “TempData”; // store data in TempData 
string name = TempData[“name”].toString(); //retrieve data from TempData 

Q19. What is Partial View in MVC?

Partial view is a type of view that can be used as a child view in other views. It is a markup file with .cshtml extension, that renders HTML output. It can be used in the layout view and other views. By reusing the same view, it eliminates the code reuse. It can also be used to break up large markup files into smaller components.

Q20. Enlist few different return types of a controller action method?

Different return types in the controller action method are,

View Result – this type returns results to the view page. It is a class that is derived from ViewResultBase class. return View();

Partial View Result – it returns partial view result to the partial view page. return PartialView();

Redirect Result – this type returns the result to a specific URL. return Redirect(“Home/Login”);

Redirect to Action Result – it returns the result to a specific controller and action. return RedirectToAction(“Login”, “Account”);

Json Result – this type returns Json data.

File Result – it returns a file in different formats.

Content Result – it returns different formats of the content to the view.

About Author :

  • Author of MVC interview questions

    Sharad Jaiswal

    Sharad Jaiswal is Sr. Web Developer from Noida area. He have rich experience in PHP, Angular Js, React, javascript and Node .If you any query or project on these programming you can drop your enquiry in comment section.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.