MVC 3 Interview Questions




Q :- What are the 3 main components of an ASP.NET MVC application?
ANS :
          1. M - Model
          2. V - View
          3. C - Controller

Q :- In which assembly is the MVC framework defined?
ANS :  System.Web.Mvc

Q :- Is it possible to combine ASP.NET webforms and ASP.MVC and develop a single web application?
ANS :  Yes, it is possible to combine ASP.NET webforms and ASP.MVC and develop a single web application.

Q :- What does Model, View and Controller represent in an MVC application?
ANS :
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.

What is the greatest advantage of using asp.net mvc over asp.net webforms?
ANS :  It is difficult to unit test UI with webforms, where views in mvc can be very easily unit tested.

Q :- Which approach provides better support for test driven development - ASP.NET MVC or ASP.NET Webforms?
ANS :  ASP.NET MVC

Q :- What are the advantages of ASP.NET MVC?
ANS :
1. Extensive support for TDD. With asp.net MVC, views can also be very easily unit tested.
2. Complex applications can be easily managed
3. Seperation of concerns. Different aspects of the application can be divided into Model, View and Controller.
4. ASP.NET MVC views are light weight, as they donot use viewstate.

Q :- Is it possible to unit test an MVC application without running the controllers in an ASP.NET process?
ANS :  Yes, all the features in an asp.net 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.

Q :- Is it possible to share a view across multiple controllers?
ANS :  Yes, put the view into the shared folder. This will automatically make the view available across multiple controllers.

Q :- What is the role of a controller in an MVC application?
ANS :  The controller responds to user interactions, with the application, by selecting the action method to execute and alse selecting the view to render.

Q :- Where are the routing rules defined in an asp.net MVC application?
ANS :  In Application_Start event in Global.asax

Q :- Name a few different return types of a controller action method?
ANS :
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

Q :- What is the significance of NonActionAttribute?
ANS : In general, all public methods of a controller class are treated as action methods. If you want prevent this default behaviour, just decorate the public method with NonActionAttribute.

Q :- What is the significance of ASP.NET routing?
ANS :  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.

Q :- What are the 3 segments of the default route, that is present in an ASP.NET MVC application?
ANS :
1st Segment - Controller Name
2nd Segment - Action Method Name
3rd Segment - Parameter that is passed to the action method

Example: http://pragimtech.com/Customer/Details/5
Controller Name = Customer
Action Method Name = Details
Parameter Id = 5

Q :- ASP.NET MVC application, makes use of settings at 2 places for routing to work correctly. What are these 2 places?
ANS :
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.

Q :- What is the adavantage of using ASP.NET routing?
ANS :  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.

Q :- What are the 3 things that are needed to specify a route?
ANS :
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.

Q :- Is the following route definition a valid route definition?
{controller}{action}/{id}
ANS :  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.

Q :- What is the use of the following default route?
{resource}.axd/{*pathInfo}
ANS :  This route definition, prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller.

Q :- What is the difference between adding routes, to a webforms application and to an mvc application?
ANS :  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.

Q :- How do you handle variable number of segments in a route definition?
ANS :  Use a route with a catch-all parameter. An example is shown below. * is referred to as catch-all parameter.
controller/{action}/{*parametervalues}

Q :- What are the 2 ways of adding constraints to a route?
ANS :
1. Use regular expressions
2. Use an object that implements IRouteConstraint interface

Q :- Give 2 examples for scenarios when routing is not applied?
ANS :
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.

Q :- What is the use of action filters in an MVC application?
ANS :  Action Filters allow us to add pre-action and post-action behavior to controller action methods.

Q :- If I have multiple filters impleted, what is the order in which these filters get executed?
ANS :
1. Authorization filters
2. Action filters
3. Response filters
4. Exception filters

Q :- What are the different types of filters, in an asp.net mvc application?
ANS :
1. Authorization filters
2. Action filters
3. Result filters
4. Exception filters

Q :- Give an example for Authorization filters in an asp.net mvc application?
ANS :
1. RequireHttpsAttribute
2. AuthorizeAttribute

Q :- Which filter executes first in an asp.net mvc application?
ANS :  Authorization filter


Q :- What are the levels at which filters can be applied in an asp.net mvc application?
ANS :
1. Action Method
2. Controller
3. Application

Q :- Is it possible to create a custom filter?
ANS : Yes

Q :- What filters are executed in the end?
ANS :  Exception Filters

Q :- Is it possible to cancel filter execution?
ANS :  Yes

Q :- What type of filter does OutputCacheAttribute class represents?
ANS :  Result Filter

Q :- What are the 2 popular asp.net mvc view engines?
ANS :
1. Razor
2. .aspx

Q :- What symbol would you use to denote, the start of a code block in razor views?
ANS :  @

Q :- What symbol would you use to denote, the start of a code block in aspx views?
ANS :  <%= %>

Q :- In razor syntax, what is the escape sequence character for @ symbol?
ANS :  The escape sequence character for @ symbol, is another @ symbol

Q :- When using razor views, do you have to take any special steps to proctect your asp.net mvc application from cross site scripting (XSS) attacks?
ANS :  No, by default content emitted using a @ block is automatically HTML encoded to protect from cross site scripting (XSS) attacks.

Q :- When using aspx view engine, to have a consistent look and feel, across all pages of the application, we can make use of asp.net master pages. What is asp.net master pages equivalent, when using razor views?
ANS :  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

Q :- What are sections?
ANS : Layout pages, can define sections, which can then be overriden by specific views making use of the layout. Defining and overriding sections is optional.

Q :- What are the file extensions for razor views?
ANS :
1. .cshtml - If the programming lanugaue is C#
2. .vbhtml - If the programming lanugaue is VB

Q :- How do you specify comments using razor syntax?
ANS :  Razor syntax makes use of @* to indicate the begining of a comment and *@ to indicate the end. An example is shown below.
@* This is a Comment *@

Also refer more important questions on Data Annotations in MVC3

link

7 comments: