All interview topics
Full-Stack, Web & Mobile

ASP.NET MVC Interview Questions and Answers

Practice ASP.NET MVC interview questions and answers organized by difficulty, with revealable explanations and related topics.

38 questionsEntry → Expert

Entry questions

2 questions
  1. Question 1
    Entry

    What is ASP.NET MVC?

    • ASP.NET MVC
    Reveal answer

    ASP.NET MVC is a web application Framework. It is light weight and highly testable Framework. MVC separates application into three components — Model, View and Controller.

  2. Question 2
    Entry

    What is Layout in MVC?

    • ASP.NET MVC
    Reveal answer

    Layout pages are similar to master pages in traditional web forms. This is used to set the common look across multiple pages. In each child page we can find

    @{
      Layout = “~/Views/Shared/TestLayout1.cshtml”;
    }
    

    This indicates child page uses TestLayout page as it’s master page.

Mid questions

15 questions
  1. Question 1
    Mid

    In OOP, what is the difference between the Repository Pattern and a Service Layer?

    • ASP.NET ASP.NET MVC Design Patterns Software Architecture ASP.NET Web API
    Reveal answer

    Repository Layer gives you additional level of abstraction over data access. Service Layer exposes business logic, which uses repository or a set of repositories as UnitOfWork In ASP.NET MVC + EF + SQL Server, I have this flow of communication: Views <- Controllers -> Service layer -> Repository layer -> EF -> SQL Server Service layer -> Repository layer -> EF This part operates on models. Views <- Controllers -> Service layer This part operates on view models.

  2. Question 1
    Mid

    What is the HelperPage.IsAjax Property?

    • ASP.NET MVC
    Reveal answer

    The HelperPage.IsAjax property gets a value that indicates whether Ajax is being used during the request of the Web page.

  3. Question 2
    Mid

    What are some of the advantages of using ASP.NET MVC vs Web Forms?

    • ASP.NET MVC
    Reveal answer

    The main advantages of ASP.NET MVC are: Enables the full control over the rendered HTML. Provides clean separation of concerns(SoC). Enables Test Driven Development (TDD). Easy integration with JavaScript frameworks. Following the design of stateless nature of the web. RESTful urls that enables SEO. No ViewState and PostBack events The main advantage of ASP.NET Web Form are: It provides RAD development Easy development model for developers those coming from winform development.

  4. Question 4
    Mid

    What is the difference between ViewBag and ViewData in MVC?

    • ASP.NET MVC
    Reveal answer

    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 .

  5. Question 5
    Mid

    What are HTML Helpers in MVC?

    • ASP.NET MVC
    Reveal answer

    HTML Helpers are like controls in traditional web forms. But HTML helpers are more lightweight compared to web controls as it does not hold viewstate and events. HTML Helpers returns the HTML string which can be directly rendered to HTML page. Custom HTML Helpers also can be created by overriding HtmlHelper class.

  6. Question 6
    Mid

    Explain the methods used to render the views in MVC?

    • ASP.NET MVC
    Reveal answer

    Below are the methods used to render the views from action: View() — To return the view from action. PartialView() — To return the partial view from action. RedirectToAction() — To Redirect to different action which can be in same controller or in different controller. Redirect() — Similar to “Response.Redirect()” in webforms, used to redirect to specified URL. RedirectToRoute() — Redirect to action from the specified URL but URL in the route table has been matched.

  7. Question 7
    Mid

    What is the HelperPage.IsAjax Property?

    • ASP.NET MVC
    Reveal answer

    The HelperPage.IsAjax property gets a value that indicates whether Ajax is being used during the request of the Web page.

  8. Question 8
    Mid

    Explain ASP.NET WebAPI vs MVC

    • ASP.NET MVC
    Reveal answer

    Usually, WebAPI used for data services where MVC can generate more types of outputs (like template HTML views). WebAPI simplifies the way we can create REST data services. It's clean and easy for this purpose. MVC can generate any output WebAPI can output. Generating outputs from templates can be easily achieved in MVC. I can't find a reason to do so in WebAPI.

  9. Question 14
    Mid

    What is Attribute Routing in MVC?

    • ASP.NET MVC
    Reveal answer

    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
    
  10. Question 15
    Mid

    Can you explain RenderBody and RenderPage in MVC?

    • ASP.NET MVC
    Reveal answer

    RenderBody is like ContentPlaceHolder in web forms. This will exist in layout page and it will render the child pages/views. Layout page will have** only one ** RenderBody() method. RenderPage also exists in Layout page and multiple RenderPage() can be there in Layout page.

  11. Question 16
    Mid

    Can you explain the page life cycle of MVC?

    • ASP.NET MVC
    Reveal answer

    Below are the processed followed in the sequence: App initialization Routing Instantiate and execute controller Locate and invoke controller action

    Instantiate and render view

  12. Question 17
    Mid

    Can a view be shared across multiple controllers? If Yes, How we can do that?

    • ASP.NET MVC
    Reveal answer

    Yes, we can share a view across multiple controllers. We can put the view in the Shared folder. When we create a new MVC Project we can see the Layout page will be added in the shared folder, which is because it is used by multiple child pages.

  13. Question 18
    Mid

    What is PartialView in MVC?

    • ASP.NET MVC
    Reveal answer

    PartialView is similar to UserControls in traditional web forms. Partial views are used for re-usability purpose . Since it's been shared with multiple views these are kept in shared folder. Partial Views can be rendered in following ways:

    Html.Partial()
    Html.RenderPartial()
    
  14. Question 19
    Mid

    Explain the difference between MVC vs ASP.NET Web API

    • ASP.NET Web API ASP.NET MVC
    Reveal answer

    The purpose of Web API framework is to generate HTTP services that reach more clients by generating data in raw format, for example, plain XML or JSON string. So, ASP.NET Web API creates simple HTTP services that renders raw data. On the other hand, ASP.NET MVC framework is used to develop web applications that generate Views (HTML) as well as data. ASP.NET MVC facilitates in rendering HTML easy.

  15. Question 20
    Mid

    What about MVC in .NET Core?

    • ASP.NET MVC .NET Core
    Reveal answer

    The ASP.NET Core MVC framework is a lightweight, open source, highly testable presentation framework optimized for use with ASP.NET Core. ASP.NET Core MVC includes the following: Routing Model binding

    Model validation Dependency injection Filters Areas Web APIs Testability Razor view engine Strongly typed views Tag Helpers View Components

Junior questions

11 questions
  1. Question 3
    Junior

    Explain Bundle.Config in MVC?

    • ASP.NET MVC
    Reveal answer

    “BundleConfig.cs” in MVC 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.

  2. Question 4
    Junior

    What are Scaffold templates in MVC?

    • ASP.NET MVC
    Reveal answer

    Scaffold templates are used to generate code for basic CRUD operations within your ASP.NET MVC applications against your database with the help Entity Framework. Microsoft provides a helpful scaffolding engine, powered by T4 templates, that automates creating basic CRUD controllers and views for models in ASP.NET MVC applications that use the Entity Framework. (There are also WebAPI and MVC without Entity Framework scaffolders currently available.)

  3. Question 5
    Junior

    What are NonAction methods in MVC?

    • ASP.NET MVC
    Reveal answer

    In MVC all public methods have been treated as Actions. So if you are creating a method and if you do not want to use it as an action method then the method has to be decorated with NonAction attribute as shown below:

    [NonAction]
    public void TestMethod()
    {
      // Method logic
    }
    
  4. Question 6
    Junior

    Explain Sections is MVC?

    • ASP.NET MVC
    Reveal answer

    Section are the part of HTML which is to be rendered in layout page. In Layout page we will use the below syntax for rendering the HTML –

    @RenderSection(“TestSection”)
    

    And in child pages we are defining these sections as shown below –

    @section TestSection{
      <h1>Test Content</h1>
    }
    
  5. Question 7
    Junior

    What is Razor Pages?

    • ASP.NET MVC
    Reveal answer

    Razor Pages is a new feature of ASP.NET Core that makes coding page-focused scenarios easier and more productive. With Razor Pages, you have this one Razor file ( .cshtml), and the code for a single page lives inside of that file, and that file also represents the URL structure of the app. Therefore, you got everything inside of one file, and it just works. However, you can separate your code to the code behind file with .cshtml.cs extension. You would usually have your view model and handlers (like action methods in MVC) in that file and handle the logic there. Of course, you could also have your view model moved to separate place. Since Razor Pages is part of the MVC stack, you can use anything that comes with MVC inside of our Razor Pages.

  6. Question 8
    Junior

    What are Actions in MVC?

    • ASP.NET MVC
    Reveal answer

    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.

  7. Question 9
    Junior

    What you mean by Routing in MVC?

    • ASP.NET MVC
    Reveal answer

    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.

  8. Question 10
    Junior

    What is the use of ViewModel in MVC?

    • ASP.NET MVC
    Reveal answer

    ViewModel 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.

  9. Question 11
    Junior

    What is Razor View Engine?

    • ASP.NET MVC
    Reveal answer

    Razor is the first major update to render HTML in MVC 3. Razor was designed specifically for view engine syntax. Main focus of this would be to simplify and code-focused templating for HTML generation. Below is the sample of using Razor: @model MvcMusicStore.Models.Customer

    @{ViewBag.Title = “Get Customers”;}
    <div class=”cust”> <h3><em>@Model.CustomerName</em> </h3>
    
  10. Question 12
    Junior

    What are the advantages of MVC over ASP.NET?

    • ASP.NET MVC
    Reveal answer

    Provides a clean separation of concerns among UI (Presentation layer), model (Transfer objects/Domain Objects/Entities) and Business Logic (Controller). Easy to UNIT Test

    Improved reusability of model and views. We can have multiple views which can point to the same model and vice versa. Improved structuring of the code

  11. Question 13
    Junior

    Can you explain Model, Controller and View in MVC?

    • ASP.NET MVC
    Reveal answer

    Model — It’s a business entity and it is used to represent the application data. Controller — Request sent by the user always scatters through controller and it’s responsibility is to redirect to the specific view using View() method. View — It’s the presentation layer of MVC.

Senior questions

9 questions
  1. Question 9
    Senior

    Is it possible to create web application with both WebForms and MVC?

    • ASP.NET ASP.NET MVC
    Reveal answer

    Yes. We have to include below mvc assembly references in the web forms application to create hybrid application.

    System.Web.Mvc System.Web.Razor System.ComponentModel.DataAnnotations

  2. Question 10
    Senior

    Why to use Html.Partial in MVC?

    • ASP.NET MVC
    Reveal answer

    This method is used to render the specified partial view as an HTML string. This method does not depend on any action methods. We can use this like below:

    @Html.Partial("TestPartialView")
    
  3. Question 11
    Senior

    What are Validation Annotations?

    • ASP.NET MVC
    Reveal answer

    Data Annotations allow us to describe the rules we want applied to our model properties, and ASP.NET MVC will take care of enforcing them and displaying appropriate messages to our users. Some Data Annotation attributes: Required – Indicates that the property is a required field DisplayName – Defines the text to use on form fields and validation messages StringLength – Defines a maximum length for a string field Range – Gives a maximum and minimum value for a numeric field Bind – Lists fields to exclude or include when binding parameter or form values to model properties ScaffoldColumn – Allows hiding fields from editor forms Consider example: namespace MvcMusicStore.Models

    {
      [Bind(Exclude = "AlbumId")]
      public class Album
      {
        [ScaffoldColumn(false)]
        public int AlbumId { get; set; }
        [DisplayName("Genre")]
        public int GenreId { get; set; }
        [DisplayName("Artist")]
        public int ArtistId { get; set; }
        [Required(ErrorMessage = "An Album Title is required")]
        [StringLength(160)]
        public string Title { get; set; }
        [Range(0.01, 100.00,
        ErrorMessage = "Price must be between 0.01 and 100.00")]
        public decimal Price { get; set; }
        [DisplayName("Album Art URL")]
        [StringLength(1024)]
        public string AlbumArtUrl { get; set; }
        public virtual Genre Genre { get; set; }
        public virtual Artist Artist { get; set; }
    
      }
    }
    
  4. Question 12
    Senior

    Describe Flux vs MVC?

    • React ASP.NET MVC
    Reveal answer

    Traditional MVC patterns have worked well for separating the concerns of data (Model), UI (View) and logic (Controller) — but MVC architectures frequently encounter two main problems: Poorly defined data flow: The cascading updates which occur across views often lead to a tangled web of events which is difficult to debug. Lack of data integrity: Model data can be mutated from anywhere, yielding unpredictable results across the UI. With the Flux pattern complex UIs no longer suffer from cascading updates; any given React component will be able to reconstruct its state based on the data provided by the store. The Flux pattern also enforces data integrity by restricting direct access to the shared data.

  5. Question 13
    Senior

    What are AJAX Helpers in MVC?

    • ASP.NET MVC
    Reveal answer

    AJAX Helpers are used to create AJAX enabled elements like as Ajax enabled forms and links which performs the request asynchronously and these are extension methods of AJAXHelper class which exists in namespace — System.Web.Mvc.

  6. Question 14
    Senior

    What is the difference between ViewData and TempData ?

    • ASP.NET MVC
    Reveal answer

    In one sentence: TempData are like ViewData with one difference: They only contain data between two successive requests, after that they are destroyed. You can use TempData to pass error messages or something similar. TempData is a session-backed temporary storage dictionary that is available for one single request. It’s great to pass messages between controllers.

  7. Question 15
    Senior

    How route table has been created in ASP.NET MVC?

    • ASP.NET MVC
    Reveal answer

    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.

  8. Question 16
    Senior

    Explain Dependency Resolution in MVC?

    • ASP.NET MVC
    Reveal answer

    Dependency Resolver 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.

  9. Question 17
    Senior

    What is Separation of Concerns in ASP.NET MVC?

    • ASP.NET MVC
    Reveal answer

    Separation of Concerns is the process of breaking the program into various distinct features which overlaps in functionality as little as possible. MVC pattern concerns on separating the content from presentation and data- processing from content.

Expert questions

1 questions
  1. Question 18
    Expert

    What is the difference between Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction ?

    • ASP.NET MVC
    Reveal answer

    Think of @Html.Partial as HTML code copied into the parent page. Think of @Html.RenderPartial as an .ascx user control incorporated into the parent page. An .ascx user control has far more overhead. Html.Partial returns a String . Html.RenderPartial calls Write internally and returns void . The basic usage is:

    // Razor syntax
    @Html.Partial("ViewName")
    @{ Html.RenderPartial("ViewName"); }// WebView syntax
    <%: Html.Partial("ViewName") %>
    <% Html.RenderPartial("ViewName"); %>
    

    In the snippet above, both calls will yield the same result. While one can store the output of Html.Partial in a variable or return it from a method, one cannot do this with Html.RenderPartial . The result will be written to the Response stream during execution/evaluation. This also applies to Html.Action and Html.RenderAction .