MVC Interview Questions and Answers

MVC Interview Questions and Answers

Last updated on 23rd Oct 2020, Blog, Interview Question

About author

Ramki ( (Sr Project Manager ) )

High level Domain Expert in TOP MNCs with 8+ Years of Experience. Also, Handled Around 20+ Projects and Shared his Knowledge by Writing these Blogs for us.

(5.0) | 12547 Ratings 2243

These MVC Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of MVC As per my experience good interviewers hardly plan to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer .we are going to cover top 100 MVC Interview questions along with their detailed answers. We will be covering MVC scenario based interview questions MVC interview questions for fresher’s as well as interview MVC questions and answers for experienced

1. What is MVC?


MVC is a framework methodology that divides an application’s implementation into three component roles: models, views, and controllers.

Main components of an MVC application?

  • M – Model
  • V – View
  • C – Controller

“Models” in a MVC based application are the components of the application that are responsible for maintaining state. Often this state is persisted inside a database (for example: we might have a Product class that is used to represent order data from the Products table inside SQL).

“Views” in a MVC based application are the components responsible for displaying the application’s user interface. Typically this UI is created off of the model data

(for example: we might create an Product “Edit” view that surfaces textboxes, dropdowns and checkboxes based on the current state of a Product object).

“Controllers” in a MVC based application are the components responsible for handling end user interaction, manipulating the model, and ultimately choosing a view to render to display UI. In a MVC application the view is only about displaying information – it is the controller that handles and responds to user input and interaction.

2. What does Model, View and Controller represent in an MVC application?


Model: Model represents the application data domain. In short the applications business logic is contained with in the model.

View: Views represent the user interface, with which the end users interact. In short the all the user interface logic is contained with in the UI.

Controller: Controller is the component that responds to user actions. Based on the user actions, the respective controller, work with the model, and selects a view to render that displays the user interface. The user input logic is contained with in the controller.

3. In which assembly is the MVC framework defined?



4. What is the greatest advantage of using mvc over webforms?


It is difficult to unit test UI with webforms, where views in mvc can be very easily unit tested.

5. Which approach provides better support for test driven development – ASP.NET MVC or ASP.NET Webforms?



6. What is Razor View Engine?


Razor view engine is a new view engine created with ASP.Net MVC model using specially designed Razor parser to render the HTML out of dynamic server side code. It allows us to write Compact, Expressive, Clean and Fluid code with new syntax to include server side code in to HTML.

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


Advantages of ASP.NET MVC:

  • Extensive support for TDD. With MVC, views can also be very easily unit tested.
  • Complex applications can be easily managed
  • Separation of concerns. Different aspects of the application can be divided into Model, View and Controller.
  • ASP.NET MVC views are light weight, as they don’t use viewstate.

8. Is it possible to unit test an MVC application without running the controllers in an ASP.NET process?


Yes, all the features in an MVC application are interface based and hence mocking is much easier. So, we don’t have to run the controllers in an ASP.NET process for unit testing.

9. What is namespace of ASP.NET MVC?


ASP.NET MVC namespaces and classes are located in the System.Web.Mvc assembly.

System.Web.Mvc namespace

Contains classes and interfaces that support the MVC pattern for ASP.NET Web applications. This namespace includes classes that represent controllers, controller factories, action results, views, partial views, and model binders.

System.Web.Mvc.Ajax namespace

Contains classes that support Ajax scripts in an ASP.NET MVC application. The namespace includes support for Ajax scripts and Ajax option settings.

System.Web.Mvc.Async namespace

Contains classes and interfaces that support asynchronous actions in an ASP.NET MVC application.

System.Web.Mvc.Html namespace

Contains classes that help render HTML controls in an MVC application. The namespace includes classes that support forms, input controls, links, partial views, and validation.

10. Is it possible to share a view across multiple controllers?


Yes, put the view into the shared folder. This will automatically make the view available across multiple controllers.

11. What is the role of a controller in an MVC application?


The controller responds to user interactions, with the application, by selecting the action method to execute and selecting the view to render.

12. Where are the routing rules defined in an MVC application?


In Application_Start event in Global.asax

13. Name a few different return types of a controller action method?


The following are just a few return types of a controller action method. In general an action method can return an instance of a any class that derives from ActionResult class.

1. ViewResult

2. JavaScriptResult

3. RedirectResult

4. ContentResult

5. JsonResult

14. What is the ‘page lifecycle’ of an ASP.NET MVC?


Following process are performed by ASP.Net MVC page:

1) App initialization

2) Routing

3) Instantiate and execute controller

4) Locate and invoke controller action

5) Instantiate and render view

15. What is the significance of NonActionAttribute?


In general, all public methods of a controller class are treated as action methods. If you want prevent this default behavior, just decorate the public method with NonActionAttribute.

16. What is the significance of ASP.NET routing?


ASP.NET MVC uses ASP.NET routing, to map incoming browser requests to controller action methods. ASP.NET Routing makes use of route table. Route table is created when your web application first starts. The route table is present in the Global.asax file.

Subscribe For Free Demo

Error: Contact form not found.

17. How route table is created in ASP.NET MVC?


When an MVC application first starts, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table.

18. What are the 3 segments of the default route, that is present in an ASP.NET MVC application?


1st Segment – Controller Name

2nd Segment – Action Method Name

3rd Segment – Parameter that is passed to the action method


Controller Name = search

Action Method Name = label

Parameter Id = MVC

19. ASP.NET MVC application, makes use of settings at 2 places for routing to work correctly. What are these 2 places?


1. Web.Config File : ASP.NET routing has to be enabled here.

2. Global.asax File : The Route table is created in the application Start event handler, of the Global.asax file.

20. What is the adavantage of using ASP.NET routing?


In an ASP.NET web application that does not make use of routing, an incoming browser request should map to a physical file. If the file does not exist, we get page not found error.

An ASP.NET web application that does make use of routing, makes use of URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user’s action and therefore are more easily understood by users.

21. What are the 3 things that are needed to specify a route?


1.URL Pattern – You can include placeholders in a URL pattern so that variable data can be passed to the request handler without requiring a query string.

2.Handler – The handler can be a physical file such as an .aspx file or a controller class.

3.Name for the Route – Name is optional.

22. Is the following route definition a valid route definition?


  • {controller}{action}/{id}

No, the above definition is not a valid route definition, because there is no literal value or delimiter between the placeholders. Therefore, routing cannot determine where to separate the value for the controller placeholder from the value for the action placeholder.

23. What is the use of the following default route?


  • {resource}.axd/{*pathInfo}

This route definition, prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller.

24. What is the difference between adding routes, to a webforms application and to an mvc application?


To add routes to a webforms application, we use MapPageRoute() method of the RouteCollection class, where as to add routes to an MVC application we use MapRoute() method.

25. How do you handle variable number of segments in a route definition?


Use a route with a catch-all parameter. An example is shown below. * is referred to as catch-all parameter.

  • controller/{action}/{*parametervalues}

26. What are the 2 ways of adding constraints to a route?


  • Use regular expressions
  • Use an object that implements IRouteConstraint interface

27. Give 2 examples for scenarios when routing is not applied?


1. A Physical File is Found that Matches the URL Pattern – This default behaviour can be overriden by setting the RouteExistingFiles property of the RouteCollection object to true.

2. Routing Is Explicitly Disabled for a URL Pattern – Use the RouteCollection.Ignore() method to prevent routing from handling certain requests.

28. What is the use of action filters in an MVC application?


Action Filters allow us to add pre-action and post-action behavior to controller action methods.

29. If I have multiple filters implemented, what is the order in which these filters get executed?


1. Authorization filters

2. Action filters

3. Response filters

4. Exception filters

30. What are the different types of filters, in an mvc application?


1. Authorization filters

2. Action filters

3. Result filters

4. Exception filters

31. Give an example for Authorization filters in an mvc application?


1. RequireHttpsAttribute

2. AuthorizeAttribute

32. Which filter executes first in an mvc application?


Authorization filter

33. What are the levels at which filters can be applied in an mvc application?


1. Action Method

2. Controller

3. Application

Course Curriculum

Take Your Career to Next Level with MVC Training to Advance Your Career

  • Instructor-led Sessions
  • Real-life Case Studies
  • Assignments
Explore Curriculum

34. Is it possible to create a custom filter?



35. What filters are executed in the end?


Exception Filters

36. Is it possible to cancel filter execution?



37. What type of filter does OutputCacheAttribute class represents?


Result Filter

38. What are the 2 popular mvc view engines?


1. Razor

2. .aspx

39. What is difference between Viewbag and Viewdata in ASP.NET MVC?


The basic difference between ViewData and ViewBag is that in ViewData instead creating dynamic properties we use properties of Model to transport the Model data in View and in ViewBag we can create dynamic properties without using Model data.

40. What symbol would you use to denote, the start of a code block in razor views?



41. What symbol would you use to denote, the start of a code block in aspx views?


<%= %>

In razor syntax, what is the escape sequence character for @ symbol?

The escape sequence character for @ symbol, is another @ symbol

42. When using razor views, do you have to take any special steps to protect your mvc application from cross site scripting (XSS) attacks?


No, by default content emitted using a @ block is automatically HTML encoded to protect from cross site scripting (XSS) attacks.

43. When using aspx view engine, to have a consistent look and feel, across all pages of the application, we can make use of master pages. What is master pages equivalent, when using razor views?


To have a consistent look and feel when using razor views, we can make use of layout pages. Layout pages, reside in the shared folder, and are named as _Layout.cshtml

44. What are sections?


Layout pages, can define sections, which can then be overriden by specific views making use of the layout. Defining and overriding sections is optional.

45. What are the file extensions for razor views?


1. .cshtml – If the programming lanugaue is C#

2. .vbhtml – If the programming lanugaue is VB

46. How do you specify comments using razor syntax?


Razor syntax makes use of @* to indicate the begining of a comment and *@ to indicate the end.

47. What is Routing?


A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. Routing module is responsible for mapping incoming browser requests to particular MVC controller actions.

48. Is it possible to combine ASP.NET webforms and ASP.MVC and develop a single web application?


Yes, it is possible to combine ASP.NET webforms and ASP.MVC and develop a single web application.

49. How do you avoid XSS Vulnerabilities in ASP.NET MVC?


Use the syntax in ASP.NET MVC instead of using .net framework 4.0.

50. Explain the new features added in version 4 of MVC (MVC4)?


Following are features added newly –

Mobile templates:

  • Added ASP.NET Web API template for creating REST based services.
  • Asynchronous controller task support.
  • Bundling the java scripts.
  • Segregating the configs for MVC routing, Web API, Bundle etc.

51. Can you explain the page life cycle of MVC?


Below are the processed followed in the sequence –

  • App initialization
  • Routing
  • Instantiate and execute controller
  • Locate and invoke controller action
  • Instantiate and render view.

52. What are the advantages of MVC over ASP.NET?


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

2. Easy to UNIT Test.

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

4. Improved structuring of the code.

53. What is Separation of Concerns in ASP.NET MVC?


It’s 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.

54. What is the meaning of Unobtrusive JavaScript?


This is a general term that conveys a general philosophy, similar to the term REST (Representational State Transfer). Unobtrusive JavaScript doesn’t intermix JavaScript code in your page markup.

Eg : Instead of using events like onclick and onsubmit, the unobtrusive JavaScript attaches to elements by their ID or class based on the HTML5 data- attributes.

55. What is the use of ViewModel in MVC?


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.

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

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

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

59. 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 });}

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

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

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

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

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

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

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

67. Can you specify different types of filters in ASP.Net MVC application?


1) Authorization filters (IAuthorizationFilter)

2) Action filters (IActionFilter)

3) Result filters (IResultFilter)

4) Exception filters (IExceptionFilter)

68. If you have already implemented different filters then what will be order of these filters?


1) Authorization filters

2) Action filters

3) Response filters

4) Exception filters

69. What are the advantages of using ASP.NET routing?



Clean URLs is originally brought from Ruby on Rails. , now clean URL in MVC ASP.Net will be work like

70. What is the difference between MVC (Model View Controller) and MVP (Model View Presenter)?



MVC controller handles all the requests, MVP handles as the handler and also handles the all requests as well.

71. Can we use third party View Engine using ASP.Net MVC Engine ?


Yes, below are the top five alternative ASP.Net MVC View Engines.

1. Spark (Castle MonoRail framework projects), Open Sourced, it is popular as MVCContrib library.

2. NHaml works like inline page templating.

3. NDjango uses F# Language.

4. Hasic uses VB.Net, XML.

5. Bellevue for ASP.NEt view, It respects HTML class first.

Course Curriculum

Best MVC Course with Industry Standard Topics From Real-Time Experts

Weekday / Weekend BatchesSee Batch Details

72. What is scaffolding using ASP.Net MVC Engine?


Scaffolding helps us to write CRUD operations blend using Entity Framework, It helps developer to write down simply even yet complex business logic.

73. What is life cycle in ASP.Net MVC Engine?


Step 1: Fill Route (Global.asax file will hit first).

Step 2: Fetch Route: It will gether information about controller and action to invoke.

Step 3: Request context

Step 4: Controller instance: it calls Controller class and method.

Step 5: Executing Action: It determines which action to be executed

Step 6: Result (View): Now Action method executed and returns back response to view in differentiating forms like Json, View Result, File Result etc.

74. Can be it possible to share single view across multiple controllers in MVC?


We can put the view under shared folder, it will automatically view the across the multiple controllers.

75. Can you list the main types of result using ASP.Net MVC?


There are total 10 main types of result, ActionResult is main type and others are sub types of results as listed below:

  • System.Web.Mvc.ActionResult
  • System.Web.Mvc.ContentResult
  • System.Web.Mvc.EmptyResult
  • System.Web.Mvc.FileResult
  • System.Web.Mvc.HttpStatusCodeResult
  • System.Web.Mvc.JavaScriptResult
  • System.Web.Mvc.JsonResult
  • System.Web.Mvc.RedirectResult
  • System.Web.Mvc.RedirectToRouteResult
  • System.Web.Mvc.ViewResultBase

76. What are Model Binders in ASP.Net MVC?


For Model Binding we will use class called : “ModelBinders”, which gives access to all the model binders in an application. We can create a custom model binders by inheriting “IModelBinder”.

77. How we can handle the exception at controller level in ASP.Net MVC?


Exception Handling is made simple in ASP.Net MVC and it can be done by just overriding “OnException” and set the result property of the filtercontext object (as shown below) to the view detail, which is to be returned in case of exception.

protected overrides void OnException(ExceptionContext filterContext)

78. What are Scaffold templates in ASP.Net MVC?


Scaffolding in ASP.NET ASP.Net MVC is used to generate the Controllers,Model and Views for create, read, update, and delete (CRUD) functionality in an application. The scaffolding will be knowing the naming conventions used for models and controllers and views.

79. Does Tempdata hold the data for other request in ASP.Net MVC?


If Tempdata is assigned in the current request then it will be available for the current request and the subsequent request and it depends whether data in TempData read or not. If data in Tempdata is read then it would not be available for the subsequent requests.

80. Explain Keep method in Tempdata in ASP.Net MVC?


As explained above in case data in Tempdata has been read in current request only then “Keep” method has been used to make it available for the subsequent request.

  • @TempData[“TestData”];
  • TempData.Keep(“TestData”);

81. Explain Peek method in Tempdata in ASP.Net MVC?


Similar to Keep method we have one more method called “Peek” which is used for the same purpose. This method used to read data in Tempdata and it maintains the data for subsequent request.

  • string A4str = TempData.Peek(“TT”).ToString();

82. What is Area in ASP.Net MVC?


Area is used to store the details of the modules of our project. This is really helpful for big applications, where controllers, views and models are all in main controller, view and model folders and it is very difficult to manage.

83. How we can register the Area in ASP.Net MVC?


When we have created an area make sure this will be registered in “Application_Start” event in Global.asax.

Below is the code snippet where area registration is done :

  • protected void Application_Start()
  • {AreaRegistration.RegisterAllAreas();}

84. What are child actions in ASP.Net MVC?


To create reusable widgets child actions are used and this will be embedded into the parent views. In ASP.Net MVC Partial views are used to have reusability in the application. Child action mainly returns the partial views.

85. How we can invoke child actions in ASP.Net MVC?


“ChildActionOnly” attribute is decorated over action methods to indicate that action method is a child action.

Below is the code snippet used to denote the child action :

  • [ChildActionOnly]
  • public ActionResult MenuBar()
  • {//Logic here
  • return PartialView();}

86. What is Dependency Injection in ASP.Net MVC?


it’s a design pattern and is used for developing loosely couple code. This is greatly used in the software projects. This will reduce the coding in case of changes on project design so this is vastly used.

87. Explain the advantages of Dependency Injection (DI) in ASP.Net MVC?


Below are the advantages of DI :

  • Reduces class coupling
  • Increases code reusing
  • Improves code maintainability
  • Improves application testing

yTDD is a methodology which says, write your tests first before you write your code. In TDD, tests drive your application design and development cycles. You do not do the check-in of your code into source control until all of your unit tests pass.

88. Explain the tools used for unit testing in ASP.Net MVC?


Below are the tools used for unit testing :

1. NUnit

2. xUnit.NET

3. Ninject 2

4. Moq

89. What is Representational State Transfer (REST) mean?


REST is an architectural style which uses HTTP protocol methods like GET, POST, PUT, and DELETE to access the data. ASP.Net MVC works in this style. In ASP.Net MVC 4 there is a support for Web API which uses to build the service using HTTP verbs.

90. How to use Jquery Plugins in ASP.Net MVC validation?


We can use dataannotations for validation in ASP.Net MVC. If we want to use validation during runtime using Jquery then we can use Jquery plugins for validation.

 Eg: If validation is to be done on customer name textbox then we can do as :

  • $(‘#CustomerName’).rules(“add”, {required: true,
  • minlength: 2,
  • messages:
  • {required: “Please enter name”,
  • minlength: “Minimum length is 2”} });

91. How we can multiple submit buttons in ASP.Net MVC?


Below is the scenario and the solution to solve multiple submit buttons issue. Scenario :

  • @using (Html.BeginForm(“MyTestAction”,”MyTestController”)
  • {<input type=”submit” value=”MySave” />
  • <input type=”submit” value=”MyEdit” />}

Solution :

Public ActionResult MyTestAction(string submit) //submit will have value either “MySave” or “MyEdit”

{// Write code here}

92. What are the differences between Partial View and Display Template and Edit Templates in ASP.Net MVC?


Display Templates : These are model centric. Meaning it depends on the properties of the view model used. It uses convention that will only display like divs or labels.

Edit Templates : These are also model centric but will have editable controls like Textboxes.

Partial View : These are view centric. These will differ from templates by the way they render the properties (Id’s) Eg : CategoryViewModel has Product class property then it will be rendered as Model.Product.ProductName but in case of templates if we CategoryViewModel has List then @Html.DisplayFor(m => m.Products) works and it renders the template for each item of this list.

MVC Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

93. Can I set the unlimited length for “maxJsonLength” property in config?


No. We can’t set unlimited length for property maxJsonLength. Default value is – 102400 and maximum value what we can set would be : 2147483644.

94. Can I use Razor code in Javascript in ASP.Net MVC?


Yes. We can use the razor code in javascript in cshtml by using <text> element.

  • < script type=”text/javascript”>
  • @foreach (var item in Model)
  • < text >

//javascript goes here which uses the server values

  • < text >
  • < script>

95. How can I return string result from Action in ASP.Net MVC?


Below is the code snippet to return string from action method :

  • public ActionResult TestAction()
  • {return Content(“Hello Test !!”);}

96. How to return the JSON from action method in ASP.Net MVC?


Below is the code snippet to return string from action method :

  • public ActionResult TestAction()
  • {return JSON(new { prop1 = “Test1”, prop2 = “Test2” });}

Are you looking training with Right Jobs?

Contact Us

Popular Courses