MVC Interview Questions Answers
MVC Interview Questions Answers
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 *@
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.
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.
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
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.
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
System.Web.Mvc namespace contains all the interfaces and classes which supports ASP.NET MVC framewor
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.
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
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.
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.
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
The
Router
Table
is
formed
by
starts,
then
the
following
method known
calls
as
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 ?
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 ?
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.