ASP.NET MVC Interview Questions and Answers
Practice ASP.NET MVC interview questions and answers organized by difficulty, with revealable explanations and related topics.
Entry questions
2 questions- Question 1Entry
What is ASP.NET MVC?
- ASP.NET MVC
Reveal answerHide 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.
- Question 2Entry
What is Layout in MVC?
- ASP.NET MVC
Reveal answerHide 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- Question 1Mid
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 answerHide 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.
- Question 1Mid
What is the HelperPage.IsAjax Property?
- ASP.NET MVC
Reveal answerHide answer
The HelperPage.IsAjax property gets a value that indicates whether Ajax is being used during the request of the Web page.
- Question 2Mid
What are some of the advantages of using ASP.NET MVC vs Web Forms?
- ASP.NET MVC
Reveal answerHide 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.
- Question 4Mid
What is the difference between ViewBag and ViewData in MVC?
- ASP.NET MVC
Reveal answerHide 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 .
- Question 5Mid
What are HTML Helpers in MVC?
- ASP.NET MVC
Reveal answerHide 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.
- Question 6Mid
Explain the methods used to render the views in MVC?
- ASP.NET MVC
Reveal answerHide 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.
- Question 7Mid
What is the HelperPage.IsAjax Property?
- ASP.NET MVC
Reveal answerHide answer
The HelperPage.IsAjax property gets a value that indicates whether Ajax is being used during the request of the Web page.
- Question 8Mid
Explain ASP.NET WebAPI vs MVC
- ASP.NET MVC
Reveal answerHide 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.
- Question 14Mid
What is Attribute Routing in MVC?
- ASP.NET MVC
Reveal answerHide 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 - Question 15Mid
Can you explain RenderBody and RenderPage in MVC?
- ASP.NET MVC
Reveal answerHide 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.
- Question 16Mid
Can you explain the page life cycle of MVC?
- ASP.NET MVC
Reveal answerHide 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
- Question 17Mid
Can a view be shared across multiple controllers? If Yes, How we can do that?
- ASP.NET MVC
Reveal answerHide 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.
- Question 18Mid
What is PartialView in MVC?
- ASP.NET MVC
Reveal answerHide 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() - Question 19Mid
Explain the difference between MVC vs ASP.NET Web API
- ASP.NET Web API ASP.NET MVC
Reveal answerHide 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.
- Question 20Mid
What about MVC in .NET Core?
- ASP.NET MVC .NET Core
Reveal answerHide 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- Question 3Junior
Explain Bundle.Config in MVC?
- ASP.NET MVC
Reveal answerHide 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.
- Question 4Junior
What are Scaffold templates in MVC?
- ASP.NET MVC
Reveal answerHide 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.)
- Question 5Junior
What are NonAction methods in MVC?
- ASP.NET MVC
Reveal answerHide 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 } - Question 6Junior
Explain Sections is MVC?
- ASP.NET MVC
Reveal answerHide 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> } - Question 7Junior
What is Razor Pages?
- ASP.NET MVC
Reveal answerHide 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.
- Question 8Junior
What are Actions in MVC?
- ASP.NET MVC
Reveal answerHide 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.
- Question 9Junior
What you mean by Routing in MVC?
- ASP.NET MVC
Reveal answerHide 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.
- Question 10Junior
What is the use of ViewModel in MVC?
- ASP.NET MVC
Reveal answerHide 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.
- Question 11Junior
What is Razor View Engine?
- ASP.NET MVC
Reveal answerHide 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> - Question 12Junior
What are the advantages of MVC over ASP.NET?
- ASP.NET MVC
Reveal answerHide 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
- Question 13Junior
Can you explain Model, Controller and View in MVC?
- ASP.NET MVC
Reveal answerHide 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- Question 9Senior
Is it possible to create web application with both WebForms and MVC?
- ASP.NET ASP.NET MVC
Reveal answerHide 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
- Question 10Senior
Why to use Html.Partial in MVC?
- ASP.NET MVC
Reveal answerHide 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") - Question 11Senior
What are Validation Annotations?
- ASP.NET MVC
Reveal answerHide 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; } } } - Question 12Senior
Describe Flux vs MVC?
- React ASP.NET MVC
Reveal answerHide 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.
- Question 13Senior
What are AJAX Helpers in MVC?
- ASP.NET MVC
Reveal answerHide 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.
- Question 14Senior
What is the difference between ViewData and TempData ?
- ASP.NET MVC
Reveal answerHide 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.
- Question 15Senior
How route table has been created in ASP.NET MVC?
- ASP.NET MVC
Reveal answerHide 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.
- Question 16Senior
Explain Dependency Resolution in MVC?
- ASP.NET MVC
Reveal answerHide 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.
- Question 17Senior
What is Separation of Concerns in ASP.NET MVC?
- ASP.NET MVC
Reveal answerHide 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- Question 18Expert
What is the difference between Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction ?
- ASP.NET MVC
Reveal answerHide 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 .