0% found this document useful (0 votes)
141 views

MVC Interview Questions Answers

The document discusses various aspects of ASP.NET MVC including its architecture and components. It provides definitions for Model, View and Controller which are the three main components of an MVC application. It also discusses advantages of ASP.NET MVC like support for test-driven development and separation of concerns. Additionally, it answers questions related to controllers, views, routing, filters and view engines in ASP.NET MVC.

Uploaded by

Kapil Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views

MVC Interview Questions Answers

The document discusses various aspects of ASP.NET MVC including its architecture and components. It provides definitions for Model, View and Controller which are the three main components of an MVC application. It also discusses advantages of ASP.NET MVC like support for test-driven development and separation of concerns. Additionally, it answers questions related to controllers, views, routing, filters and view engines in ASP.NET MVC.

Uploaded by

Kapil Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

MVC Interview Questions Answers

What are the 3 main components of an ASP.NET MVC application?


1. M - Model
2. V - View
3. C - Controller
In which assembly is the MVC framework defined?
System.Web.Mvc
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.
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.
What is the greatest advantage of using asp.net mvc over asp.net webforms?
It is difficult to unit test UI with webforms, where views in mvc can be very easily unit tested.
Which approach provides better support for test driven development - ASP.NET MVC or ASP.NET
Webforms?
ASP.NET MVC
What are the advantages of ASP.NET MVC?
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.
Is it possible to unit test an MVC application without running the controllers in an ASP.NET process?
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.
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.
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
alse selecting the view to render.
Where are the routing rules defined in an asp.net MVC application?
In Application_Start event in Global.asax

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
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 behaviour, just decorate the public method with NonActionAttribute.
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.
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
Example: http://dotnetcodes.com/Article/id/5
Controller Name = Article
Action Method Name = id
Parameter Id = 5
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.
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.
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.
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.


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.
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.
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}
What are the 2 ways of adding constraints to a route?
1. Use regular expressions
2. Use an object that implements IRouteConstraint interface
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.
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.
If I have multiple filters impleted, what is the order in which these filters get executed?
1. Authorization filters
2. Action filters
3. Response filters
4. Exception filters
What are the different types of filters, in an asp.net mvc application?
1. Authorization filters
2. Action filters
3. Result filters
4. Exception filters
Give an example for Authorization filters in an asp.net mvc application?
1. RequireHttpsAttribute
2. AuthorizeAttribute
Which filter executes first in an asp.net mvc application?
Authorization filter

What are the levels at which filters can be applied in an asp.net mvc application?
1. Action Method
2. Controller

3. Application
[b]Is it possible to create a custom filter?[/b]
Yes
What filters are executed in the end?
Exception Filters
Is it possible to cancel filter execution?
Yes
What type of filter does OutputCacheAttribute class represents?
Result Filter
What are the 2 popular asp.net mvc view engines?
1. Razor
2. .aspx
What symbol would you use to denote, the start of a code block in razor views?
@
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
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?
No, by default content emitted using a @ block is automatically HTML encoded to protect from cross site
scripting (XSS) attacks.
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?
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
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.
What are the file extensions for razor views?
1. .cshtml - If the programming lanugaue is C#
2. .vbhtml - If the programming lanugaue is VB
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. An example
is shown below.
@* This is a Comment *@

Asp.net MVC interview questions and answers


ASP.NET MVC - Explaining MVC
Difference between ASP.NET MVC and ASP.NET WebForms
ASP.NET MVC architecture - different from others
ASP.NET MVC - Advantages of MVC over WebForms
ASP.NET MVC - Request Flow
Procedure to create environment for ASP.NET
ASP.NET MVC procedure to handle the process request using MHPM events
ASP.NET MVC flow to process of the request
Features making ASP.NET more used framework
ASP.NET MVC folder conventions
Function of New View Engine in ASP.NET
Function of Razor
ASP.NET MVC program - Code nuggets to create a simple application
Program in ASP.NET MVC- Razor view engine to create a simple application
Namespace classes in ASP.NET MVC
Repository Pattern in ASP.NET MVC
Difference between MVC and MVP
Page lifecycle of an ASP.NET MVC
Program to call the js function - change in dropdown list made in ASP.NET MVC
Function of URL routing system in ASP.NET MVC

MVC means ?
Posted by: Bharathi Cherukuri

MVC
It
divides
These
i)

Models

Example:

stands
an
application
component

:
we

These
might

into

component
have

roles

Product

for
component
are

3
roles
are

class

used
that

is

to
used

Model
which
is
discussed

roles
maintain
to

the

represent

based

state

which

order

data

fr

ii) Views : These component roles are used to display the user interface of the application, where

Example: we might create an Product Edit view that surfaces textboxes, dropdowns and checkboxes bas

iii) Controllers : These component roles are used for various purposes like handling end user interacti
choosing
a
view
to
render
t
Note:

In a MVC application, the views are used only for displaying the information whereas the controllers are us
and interaction.

Which assembly is used to define the MVC framework and Why ?


Posted by: Bharathi Cherukuri

The
MVC
framework
is
defined
through
This is because this is the only assembly which contains classes and interfaces that support the ASP.NET
creating Web applications.

How can we plug an ASP.NET MVC into an existing ASP.NET application ?


Posted by: Bharathi Cherukuri

We

can

First

of

combine

all,

you

ASP.NET
have

to

MVC
add

into

an

reference

existing
to

the

ASP.NET
following

application

three

by

assemblies

to

i)
ii)
iii)
The
Add
And
For

ASP.NET
MVC
folder
the
folder
Controllers,
then
you
have
this
you

should
be
created
Views,
and
Views
|
to
do
the
can
refer

after
Shared
necessary
to

to

adding
you
chang

http://www.packtpub.com/article/mixing-aspnet-webforms-and-aspnet-mvc

What are the advantages of using asp.net mvc ?


Posted by: Bharathi Cherukuri

The

main

advantages

of

using

asp.net

mvc

i) One of the main advantage is that it will be easier to manage the complexity as the application
ii) It gives us the full control over the behavior of an application as it does not us
iii)
It
provides
better
support
for
test-drive
iv) You can design the application with a rich routing infrastructure as it uses a Front Controller pattern that
a single controller.

Explain about Razor View Engine ?


Posted by: Bharathi Cherukuri

This
Razor
View
engine
is
a
part
of
new
rendering
framewor
ASP.NET rendering engine uses opening and closing brackets to denote code (<% %>), whereas Razor allo
where
code
blocks
start

Example:
In

the

classic

renderer

<ul>
<% foreach (var userTicket in Model)
{ %>
<li><%: userTicket.Value %></li>
<%

} %>

</ul>

By

using

<ul>
@foreach (var userTicket in Model)
{
<li>@userTicket.Value</li>
}
</ul>

Does the unit testing of an MVC application is possible without running con
process ?
Posted by: Bharathi Cherukuri

In an MVC
application, all the features are
based on interface.
So, it is
And it is to note that, in MVC application there is no need of running the controllers for unit testing.

easy

Which namespace is used for ASP.NET MVC ?


Posted by: Bharathi Cherukuri

System.Web.Mvc namespace contains all the interfaces and classes which supports ASP.NET MVC framewor

Can we share a view across multiple controllers ?


Posted by: Bharathi Cherukuri

Yes,

It

is

possible

to

share

view

across

multiple

controllers

by

putting

By doing like this, you can automatically make the view available across multiple controllers.

What is the use of a controller in an MVC applicatio ?


Posted by: Bharathi Cherukuri

controller

will

decide

what

to

do

and

what

to

i)
A
request
will
be
ii)
Basing
on
the
request
parameters,
iii)
Basing
on
the
request
parameters,
it
iv) Then it will delegate the next view to be shown

it
will

display

in

received
will
delegates

the

decide
the

Mention some of the return types of a controller action method ?


Posted by: Bharathi Cherukuri

An
action
method
is
used
to
return
an
instance
of
any
class
which
is
Some
of
the
return
types
of
a
controller
i)
ViewResult
:
It
is
used
to
return
a
webpage
ii)
PartialViewResult
:
It
is
used
to
send
a
section
of
a
view
to
b
iii)
JavaScriptResult
:
It
is
used
to
return
JavaScript
code
which
will
be
iv)
RedirectResult
:
Based
on
a
URL,
It
is
used
to
redirect
to
anothe
v) ContentResult : It is an HTTP content type may be of text/plain. It is used to return a custom cont
vi)
JsonResult
:
It
is
used
to
return
a
message
which
vii)
FileResult
:
It
is
used
to
send
binary
o
viii) EmptyResult : It returns nothing as the result.

Explain about 'page lifecycle' of ASP.NET MVC ?


Posted by: Bharathi Cherukuri

The

page

lifecycle

of

an

ASP.NET

MVC

i)
App
In
this
stage,
the
aplication
starts
up
by
In
this
method,
you
can
add
Route
objects
If youre implementing a custom IControllerFactory, you can set this
System.Web.Mvc.ControllerFactory.Instance
ii)
Routing
is
MvcHandler is,
iii)
At
iv)
At

a
stand-alone
component
itself, an IHttpHandler, which

this
this

stage,

stage,
the

Instantiate
the

Locate
controller

invokes

that
matches
acts as a kind

active
its

and
relevant

of

running
to
as the

is

Global.a
the
st
active con

incoming
requests
to
proxy to other IHttpHan

and
IControllerFactory
action

page

Ex
supplies

invoke
method,
which

after

fu

v)
Instantiate
and
At this stage, the IViewFactory supplies an IView, which pushes response data to the IHttpResponse object.

Explain about NonActionAttribute ?


Posted by: Bharathi Cherukuri

It
is
already
known
that
all
the
public
methods
of
a
controller
class
are
ba
If you dont want this default behaviour, then you can change the public method with NonActionAttribute. Th

Explain about the formation of Router Table in ASP.NET MVC ?


Posted by: Bharathi Cherukuri

The

Router

Table

is

formed

In the begining stage, when the ASP.NET application


The
Application_Start()
will
This RegisterRoutes() method will create the Router table.

by
starts,
then

the

following
method known
calls

as

In an MVC application, what are the segments of the default route ?


Posted by: Bharathi Cherukuri
There are 3 segments of the default route in an MVC application.They are:
The 1st segment is the Controller Name.
Example: search
The 2nd segment is the Action Method Name.
Example: label
The 3rd segment is the Parameter passed to the Action method.
Example: Parameter Id - MVC
What are the settings to be done for the Routing to work properly in an MVC application ?
Posted by: Bharathi Cherukuri
The settings must be done in 2 places for the routing to work properly.They are:
i) Web.Config File : In the web.config file, the ASP.NET routing has to be enabled.
ii) Global.asax File : The Route table is created in the application Start event handler, of the Global.asax file.
How to avoid XSS Vulnerabilities in ASP.NET MVC ?

Posted by: Bharathi Cherukuri


To avoid xss vulnerabilities, you have to use the syntax as '<%: %>' in ASP.NET MVC instead of using the syntax as '<%= %>' in .ne
This is because it does the HTML encoding.
Example:

<input type="text" value="<%: value%>" />


Explain the advantages of using routing in ASP.NET MVC ?

Posted by: Bharathi Cherukuri


Without using Routing in an ASP.NET MVC application, the incoming browser request should be mapped to a physical file.The thing is t
found error.
By using Routing, it will make use of URLs where there is no need of mapping to specific files in a web site.
This is because, for the URL, there is no need to map to a file, you can use URLs that are descriptive of the user's action and therefore
What are the things that are required to specify a route ?

Posted by: Bharathi Cherukuri


There are 3 things that are required to specify a route.

i) URL Pattern : You can pass the variable data to the request handler without using a query string. This is done by including placehold
ii) Handler : This handler can be a physical file of 2 types such as a .aspx file or a controller class.
iii) Name for the Route : This name is an optional thing.
Is the route {controller}{action}/{id} a valid route definition or not and why ?

Posted by: Bharathi Cherukuri


{controller}{action}/{id} is not a valid route definition.
The reason is that, it has no literal value or delimiter between the placeholders.
If there is no literal, the routing cannot determine where to seperate the value for the controller placceholder from the value for the ac
Explain about default route, {resource}.axd/{*pathInfo} ...

Posted by: Bharathi Cherukuri


With the help of this default route {resource}.axd/{*pathInfo}, you can prevent requests for the web resources files such as WebReso
controller.

Explain the difference between adding routes, to a web application and to an mvc application ?
Posted by: Bharathi Cherukuri
We use MapPageRoute() method of a RouteCollection class for adding routes to a webforms application, whereas MapRoute() method
Is there any way to handle variable number of segments in a route definition ?
Posted by: Bharathi Cherukuri
You can handle variable number of segments in a route definition by using a route with a catch-all parameter.
Example:
controller/{action}/{*parametervalues}
Here * reffers to catch-all parameter.
How do you add constraints to a route ?
Posted by: Bharathi Cherukuri
There are 2 ways for adding constraints to a route. They are:
i) By using Regular Expressions and
ii) By using an object which implements IRouteConstraint interface.
Explain with examples for scenarios when routing is not applied ?
Posted by: Bharathi Cherukuri
Below are the 2 scenarios where routing is not applied.

i) A Physical File is Found that Matches the URL Pattern - This default behaviour can be overriden by setting the RouteExistingFiles pro
ii) Routing Is Explicitly Disabled for a URL Pattern - By using the RouteCollection.Ignore() method, you can prevent routing from hand
Explain the usage of action filters in an MVC application ?

Posted by: Bharathi Cherukuri


Action filter in an mvc application is used to perform some additional processing, such as providing extra data to the action method, in
action method.
Action filter is an attribute that implements the abstract FilterAttribute class.

In an MVC application, which filter executes first and which is the last ?
Posted by: Bharathi Cherukuri
As there are different types of filters in an MVC application, the Authorization filter is the one which executes first and Exeption filters
Difference between Viewbag and Viewdata in ASP.NET MVC ?
Posted by: Bharathi Cherukuri
Both are used to pass the data from controllers to views.
The difference between Viewbag and Viewdata in ASP.NET MVC is explained below:
View Data:
In this, objects are accessible using strings as keys.
Example:
In the Controller:
public ActionResult Index()
{
var softwareDevelopers = new List<string>
{
"Brendan Enrick",
"Kevin Kuebler",
"Todd Ropog"
};
ViewData["softwareDevelopers"] = softwareDevelopers;
return View();
}
In the View:
<ul>
@foreach (var developer in (List<string>)ViewData["softwareDevelopers"])
{
<li>
@developer
</li>
}
</ul>

An important note is that when we go to use out object on the view that we have to cast it since the ViewData is storing everything as
View Bag:
It will allow the objectto dynamically have the properties add to it.
Example:
In the Controller:
public ActionResult Index()
{
var softwareDevelopers = new List<string>
{
"Brendan Enrick",
"Kevin Kuebler",
"Todd Ropog"
};
ViewBag.softwareDevelopers = softwareDevelopers;
return View();
}
In the View:
<ul>
@foreach (var developer in ViewBag.softwareDevelopers)
{
<li>
@developer

</li>
}
</ul>

An important point to note is that there is no need to cast our object when using the ViewBag. This is because the dynamic we used le
What are the file extensions for razor views ?
Posted by: Bharathi Cherukuri
There are two types of file extensions for razor views.
They are:
i) .cshtml : This file extension is used, when the programming language is a C#.
ii) .vbhtml : This file extension is used, when the programming language is a VB.
How can you specify comments using razor syntax ?
Posted by: Bharathi Cherukuri
In razor syntax, we use the below shown symbols for specifying comments.
i) For indicating the begining of a comment, we use @* syntax.
ii) For indicating the end of a comment, we use *@ syntax.

Explain the usage of validation helpers in ASP.NET MVC ?

Posted by: Bharathi Cherukuri


In ASP.NET MVC, validation helpers are used to show the validation error messages in a view.
The Html.ValidationMessage() and Html.ValidationSummary() helpers are used in the automatically generated Edit and Create views b
To create a view, the following steps are to be followed:
i) In the product controller,right click on the Create() action and then select the option Add View.
ii) Now check the checkbox labeled in the Add View dialog and create a strongly-typed view.
iii) Select the product class from the View data class dropdownlist.
iv) Now, select Create from the view content dropdownlist.
v) Finally, click on the Add button.

Is it possible to create a MVC Unit Test application in Visual Studio Standard ?


Posted by: Bharathi Cherukuri
No, It is not possible to create a MVC Unit Test application in Visual Studio Standard.
It is also not possible to create in Visual Studio Express.
It is possible to create only in Visual Studio Professional and Visual Studio Enterprise Versions.

You might also like