Top 20 MVC Interview Questions and Answers

Shailendra Chauhan  Print   10 min read  
11 May 2019
28 Jun 2019
2.32K

MVC stands for Model-View-Controller. It is a software design pattern which was introduced in the 1970s. Also, MVC pattern forces a separation of concerns, it means domain model and controller logic are decoupled from the user interface (view). As a result, maintenance and testing of the application become simpler and easier.

  1. What is MVC?

    MVC stands for Model-View-Controller. It is a software design pattern which was introduced in the 1970s. Also, MVC pattern forces a separation of concerns, it means domain model and controller logic are decoupled from the user interface (view). As a result, maintenance and testing of the application become simpler and easier.

  2. What is ASP.NET MVC?

    ASP.NET MVC is an open source framework built on the top of the Microsoft .NET Framework to develop a web application that enables a clean separation of code. ASP.NET MVC framework is the most customizable and extensible platform shipped by Microsoft.

  3. What are the advantages of ASP.NET MVC?

    There are following advantages of ASP.NET MVC over Web Forms (ASP.NET):

    • Separation of concern: MVC design pattern divides the ASP.NET MVC application into three main aspects Model, View, and Controller which make it easier to manage the application complexity.

    • TDD: The MVC framework brings better support to test-driven development.

    • Extensible and pluggable: MVC framework components were designed to be pluggable and extensible and therefore can be replaced or customized easier than Web Forms.

    • Full control over application behavior: MVC framework doesn’t use View State or server-based forms like Web Forms. This gives the application developer more control over the behaviors of the application and also reduces the bandwidth of requests to the server.

    • ASP.NET features are supported MVC framework is built on top of ASP.NET and therefore can use most of the features that ASP.NET include such as the provider's architecture, authentication, and authorization scenarios, membership, and roles, caching, session and more.

    • URL routing mechanism: MVC framework supports a powerful URL routing mechanism that helps to build a more comprehensible and searchable URL in your application. This mechanism helps the application to be more addressable from the eyes of search engines and clients and can help in search engine optimization.

  4. What is Routing in ASP.NET MVC?

    Routing is a pattern matching system that monitors the incoming request and figures out what to do with that request. At runtime, the Routing engine uses the Route table for matching the incoming request's URL pattern against the URL patterns defined in the Route table. You can register one or more URL patterns to the Route table at Application_Start event. When the routing engine finds a match in the route table for the incoming request's URL, it forwards the request to the appropriate controller and action. If there is no match in the route table for the incoming request's URL, it returns a 404 HTTP status code.

  5. When to use Attribute Routing?

    The convention-based routing is complex to support certain URI patterns that are common in RESTful APIs. But by using attribute routing you can define these URI patterns very easily.

    For example, resources often contain child resources like Clients have ordered, movies have actors, books have authors and so on. It’s natural to create URIs that reflects these relations like as /clients/1/orders

    This type of URI is difficult to create using convention-based routing. Although it can be done, the results don’t scale well if you have many controllers or resource types. With attribute routing, it’s pretty much easy to define a route for this URI. You simply add an attribute to the controller action as:

     [Route("clients/{clientId}/orders")]
     public IEnumerable<Order> GetOrdersByClient(int clientId)
     {
     //TO DO
     }
    
  6. How to enable Attribute Routing in ASP.NET MVC?

    Enabling attribute routing in your ASP.NET MVC5 application is simple, just add a call to routes.MapMvcAttributeRoutes() method within RegisterRoutes() method of RouteConfig.cs file.

  7. What is the difference between Routing and URL Rewriting?

    Many developers compare routing to URL rewriting since both look similar and can be used to make SEO friendly URLs. But both the approaches are very much different. The main difference between routing and URL rewriting is given below :

    • URL rewriting is focused on mapping one URL (new URL) to another URL (old URL) while routing is focused on mapping a URL to a resource.

    • URL rewriting rewrites your old URL to a new one while routing never rewrite your old URL to the new one but it maps to the original route.

  8. What is View Engine?

    A View Engine is an MVC subsystem which has its own markup syntax. It is responsible for converting the server-side template into HTML markup and rendering it to the browser. Initially, ASP.NET MVC ships with one view engine, web forms (ASPX) and from ASP.NET MVC3 a new view engine, Razor is introduced. With ASP.NET MVC, you can also use other view engines like Spark, NHaml, etc.

    What is Razor View Engine?

    Razor Engine is an advanced view engine that was introduced with MVC3. This is not a new language but it is a new markup syntax. Razor has new and advances syntax that is compact, expressive and reduces typing. Razor syntax is easy to learn and much cleaner than Web Form syntax. Razor uses @ symbol to write markup as:

     @Html.ActionLink("SignUp", "SignUp")
    
  9. What are HTML Helpers in ASP.NET MVC?

    An HTML Helper is just a method that returns an HTML string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input>, <button> and <img> tags etc. You can also create your own HTML Helpers to render more complex content such as a menu strip or an HTML table for displaying database data.

  10. What are Url Helpers?

    Url helpers allow you to render HTML links and raw URLs. The output of these helpers is dependent on the routing configuration of your ASP.NET MVC application.

  11. When to use _ViewStart?

    When a set of views shares common settings, the _ViewStart.cshtml file is a great place to put these common view settings. If any view needs to override any of the common settings then that view can set new values in common settings.

  12. Can you change the action method name?

    You can also change the action method name by using the ActionName attribute. Now the action method will be called by the name defined by the ActionName attribute.

     
     [ActionName("DoAction")]
     public ActionResult DoSomething()
     {
     //TODO:
     return View();
     }
    

    Now, DoSomething action will be identified and called by the name DoAction.

  13. What are Data Annotations in ASP.NET MVC?

    Data validation is a key aspect of developing a web application. In Asp.net MVC, we can easily apply validation to the web application by using Data Annotation attribute classes to the model class. Data Annotation attribute classes are present in System.ComponentModel.DataAnnotations namespace and are available to Asp.net projects like Asp.net web application & website, Asp.net MVC, Web forms and also to Entity framework ORM models. Data Annotations help us to define the rules to the model classes or properties for data validation and displaying suitable messages to end users.

  14. How to determine there is no error in Model State?

    When server-side model validation fails, errors are included in the ModelState. Hence, by using ModelState.IsValid property you can verify model state. It returns true if there is no error in ModelState else returns false.

     [HttpPost]
     public ActionResult DoSomething(UserViewModel model)
     {
     if (ModelState.IsValid)
     {
     //TODO:
     }
     return View();
     }
    
  15. What is jQuery Validation Unobtrusive plugin?

    Microsoft introduced jquery.validate.unobtrusive.js plugin with ASP.NET MVC3 to apply data model validations to the client side using a combination of jQuery Validation and HTML 5 data attributes.

  16. Can we use Bundling and Minification in ASP.NET MVC3 or ASP.NET4.0?

    System.Web.Optimization class offers the bundling and minification techniques that exist within Microsoft.Web.Optimization dll. Using this dll you can also use this technique with ASP.NET MVC3 and ASP.NET 4.0.

  17. What is Scaffolding?

    Scaffolding is a technique used by many MVC frameworks like ASP.NET MVC, Ruby on Rails, Cake PHP and Node.JS, etc., to generate code for basic CRUD (create, read, update, and delete) operations against your database effectively. Further, you can edit or customize this auto-generated code according to your need. 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 Web site.

  18. How to persist data in TempData?

    The life of TempData is very short and lies only until the target view is fully loaded. But you can persist data in TempData by calling Keep() method after request completion

  19. Can you change the action method name?

    You can also change the action method name by using the ActionName attribute. Now the action method will be called by the name defined by the ActionName attribute.

     
     [ActionName("DoAction")]
     public ActionResult DoSomething()
     {
     //TODO:
     return View();
     }
    

    Now, DoSomething action will be identified and called by the name DoAction.

Summary

I hope the above questions and answers will help you with your ASP.NET MVC Interview. All the above interview Questions have been taken from our new released eBook ASP.NET MVC Interview Questions and Answers.

This eBook has been written to make you confident in ASP.NET MVC with a solid foundation. Also, this will help you to use ASP.NET MVC in your real project.

Buy this eBook at a Discounted Price!

ASP.NET MVC Interview Questions and Answers eBook

Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.

Learn to Crack Your Technical Interview

+
+
Accept cookies and close this message