ASP.Net MVC Interview Questions and Answers
- by Samir R. Bhogayta
About
ASP.Net MVC
The
ASP.Net MVC is the framework provided by Microsoft that lets you develop the
applications that follows the principles of Model-View-Controller (MVC) design
pattern. The .Net programmers new to MVC thinks that it is similar to WebForms Model (Normal ASP.Net), but it is far
different from the WebForms programming.
This article will tell you how to
quick learn the basics of MVC along with some frequently asked interview
questions and answers on ASP.Net MVC
1.
What is ASP.Net MVC
The ASP.Net MVC is
the framework provided by Microsoft to achieve
separation of concerns that leads to easy maintainability of
the application.
Model is supposed to handle data
related activity
View deals with user
interface related work
Controller is meant for managing the
application flow by communicating between Model and View.
Normal
0
false
false
false
EN-US
X-NONE
X-NONE
2. Why to use ASP.Net MVC
The strength of MVC (i.e. ASP.Net MVC)
listed below will answer this question
MVC reduces the
dependency between the components; this makes your code more testable.
MVC does not
recommend use of server controls, hence the processing time required to
generate HTML response is drastically reduced.
The integration of
java script libraries like jQuery, Microsoft MVC becomes easy as compared to
Webforms approach.
3. What do you mean by Razor
The Razor is the new View engine introduced
in MVC 3.0.
The View engine is responsible for processing the view files [e.g. .aspx,
.cshtml] in order to generate HTML response.
The previous versions of MVC were dependent on ASPX view engine.
4. Can we use ASPX view engine in
latest versions of MVC
Yes. The Recommended way is to prefer
Razor View
5. What are the benefits of
Razor View?
The syntax for server
side code is simplified
The length of code is
drastically reduced
Razor syntax is easy
to learn and reduces the complexity
Normal
0
false
false
false
EN-US
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}
6. What is the extension of Razor
View file?
.cshtml (for c#) and .vbhtml (for vb)
7. How to create a Controller in MVC
Normal
0
false
false
false
EN-US
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}
Create a simple class and extend it
from Controller class. The bare minimum requirement for a class to become a controller is to inherit it from ControllerBase is the class that is required to
inherit to create the controller but Controller class inherits from ControllerBase.
8. How to create an Action method in MVC
Add a simple method inside a controller
class with ActionResult return type.
9. How to access a view on the
server
The browser generates the request in
which the information like Controller name, Action Name and Parameters are
provided, when server receives this URL it resolves the Name of Controller and
Action, after that it calls the specified action with provided parameters. Action
normally does some processing and returns the ViewResult by specifying the view
name (blank name searches according to naming conventions).
10. What is the default Form method
(i.e. GET, POST) for an action method
GET.
To change this you can add an action level attribute e.g [HttpPost]
Normal
0
false
false
false
EN-US
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}
11. What is a Filter in MVC?
When user (browser) sends a request to server
an action method of a controller gets invoked; sometimes you may require executing
a custom code before or after action method gets invoked, this custom code is
called as Filter.
12. What are the different types of Filters
in MVC?
a. Authorization filter
b. Action filter
c. Result filter
d. Exception filter
[Do not forget the order mentioned
above as filters gets executed as per above mentioned sequence]
13. Explain the use of Filter with
an example?
Suppose you are working on a MVC application
where URL is sent in an encrypted format instead of a plain text, once encrypted
URL is received by server it will ignore action parameters because of URL
encryption.
To solve this issue you can create
global action filter by overriding OnActionExecuting method of controller class, in this you
can extract the action parameters from the encrypted URL and these parameters
can be set on filterContext to send plain text parameters to the actions.
14. What is a HTML helper?
A HTML helper is a method that returns string;
return string usually is the HTML tag. The standard HTML helpers (e.g. Html.BeginForm(),Html.TextBox())
available in MVC are
lightweight as it does not rely on event model or view state as that of in ASP.Net
server controls.