Wednesday, July 20, 2016

ASP.NET MVC Interview Questions and Answers

ASP.NET MVC is getting popular more and more day by day, it has different features, it is popular due to its Modal-View-Controller architecture.
This article contains ASP.NET MVC interview questions that will help you to brush up your asp.net MVC concepts, this is the one point stop where you can get interview breaker Q & A.

Let's go through it

1. What is ASP.NET MVC ?
This is the most common asked question, it is an architectural pattern which separates the UI and code. It's basically divided into Model, View, and Controller section.
- As name suggest, The View is responsible for the look and feel.
- Model is real world object and provides data to the View.
- This is the heart of the architecture, it responsible for request and loading the appropriate Model and View.


2. What are the features of ASP.NET MVC?
- Clear separation of application concerns.
- Supports for Test Driven Development
- Supports Membership and roles, authentication and authorization, provider model and caching

3. What is Routing in ASP.NET MVC?
Routing is very easy in ASP.NET MVC. it shows user friendly URL in application, it is just like a URL rewriting. ASP.NET MVC framework uses a routing engine, that maps URLs to controller classes
see below image in which you can see difference between ASP.NET path and ASP.NET MVC path, We can define routing rules for the engine, so that it can map incoming request URLs to appropriate controller
When URL redirect to another page it collect routing rules that are defined in Global.asax file and process the request

4. How to navigate from One view to another ?
You can easily do it using Hyperlink, by using 'ActionLink' method we can navigate to destination action
see below snippet
<%= Html.ActionLink("Home","GotoDestination") %>

5. What is VIEWDATA in ASP.NET MVC ?
To pass data from controller to view and in next subsequent request, ASP.NET MVC uses VIEWDATA. But it is limited to only server call and to main the state. When i redirect to another page
it gives me 'null' value, it acts between a link in VIEW and CONTROLLER. ViewData being a dictionary object is accessible using strings as keys.
ViewData requires typecasting for complex data type and check for null values to avoid error.
see below sample
//IN CONTROLLER
//add some value to VIEWDATA
public ActionResult Index()
{
 ViewData["Text"] = "This is from view data";
 return View();
}

//IN VIEW
//access it in dictonary
@ViewData["Text"]

6. What is VIEWBAG in ASP.NET MVC ?
It is simply same as VIEWDATA, which used to maintain state and transfer values between controller to view and in next subsequent request, just the difference is ViewBag is a dynamic property that takes advantage of the new dynamic features
ViewBag doesn't require typecasting for complex data type, both VIEWDATA and VIEWBAG has short life means, they are 'null' if redirect to another page
see below sample

//IN CONTROLLER
//add some value to VIEWDATA
public ActionResult Index()
{
 ViewBag.Name = "This is from VIEWBAG";
 return View();
}

//IN VIEW
//access it
@ViewBag.Name 
7. What is TEMPDATA in ASP.NET MVC ?
TEMPDATA is also dictionary type but it access request data and maintain state between multiple redirect, from one controller to the other controller or from one action to other action.
it uses HTTP request to transfer data. It internally uses session variables. it can be used to store only one time messages like exceptions.


see below sample
//in controller 1
public ActionResult Index()
{
 var model = new Review()
 {
 Body = "i am controller 1",
 };
 TempData["CON1"] = model;
 return RedirectToAction("Test");
}

//in controller 2
public ActionResult Test() 
{ 
 var model= TempData["CON1"]; 
 return View(model); 
}
8. How to transfer values in ASP.NET MVC between Controller and View ?
There are following ways to transfer values in ASP.NET MVC
1. ViewData : Transfer values only between Controller to View
2. ViewBag : Transfer values only between Controller to View
3. TempData : Transfer values only between Controller to Controller
4.Session Variables : It is very strong and dynamic way to Transfer values in ASP.NET MVC, it supports values transfer between Controller to Controller, Controller to View and View and Controller
5. Hidden Fields : Transfer values only between View to Controller

9. Is TempData can persist values between multiple request ?
This is very weird question The answer of this question is YES and NO,
YES cause it can persist values between request and NO cause once the value of TempData is read, it is empty

10. What is Model ?
Model is the layer between UI and input, in simple ways it handles all business logic, validation and database part, it manipulates it and saved back to database

11. Can i use ASP.NET MVC in Windows application
No. Model, View, controller framework is used to create web applications only, in windows you can Model, View, Presenter model

12. What is Razor in ASP.NET MVC ?
As word suggest Razor is acts like cleaner, it just trim the coding brackets and allow new style of coding
see below snippet

//in Classic ASP.NET we uses brackets to render variable
<ol>
<% foreach (var empname in Model)
{ %>
 <li> <% empname.name %> </li>
<% }%>
</ol>

//in ASP.MVC, Razor cleans unused tags and make it faster
<ol>
@foreach (var empname in Model)
{ 
 <li> @empname.name </li>
}
</ol>
13. What is partial view
It is like a user control in asp.net we can use them multiple time, its a reusable view.Partial and RenderPartial methods are used to show partial view
see below snippet
<div>@Html.Partial("_Test")</div>
<div>@{Html.RenderPartial("_Test")}</div>
14. What are the different result ASP.NET MVC can produce
Basically ASP.NET MVC produce only one result, that is 'ActionResult' but it has again different sub class like,
1. ViewResult - Get the response and render specific view.
2. PartialViewResult - Renders a specific but partial view to the response
3. EmptyResult - Return an empty result
4. RedirectResult - Returns an HTTP redirection to a given URL
5. RedirectToRouteResult - Depending on the routing engine get the URL and redirect the result
6. JsonResult - Return given ViewData object to JSON format
7. JavaScriptResult - Return code piece of Javascript that can later execute on client machine
8. ContentResult - Return content to the response
9. FileContentResult - Returns a file with content to the client
10.FileStreamResult - Returns a file to the client but in the form of a Stream
11.FilePathResult - Returns a file with path to the client

15. How to add style sheet in ASP.NET MVC
it is same like your classic ASP.NET
<link rel="StyleSheet" href="@Href(test.css)" type="text/css" />
16.What is the HTML Helper in ASP.NET MVC ?
HTML Helper is like a Classic ASP.NET controls but they are very light weight in comparison with traditional controls, an HTML helper does not have an event model and a view state.
all HTML links and HTML form elements are the part of HTML helper class
e.g. see below HTML link control in HTML Helper

@Html.ActionLink("Check this Website", "Newwebsite")
//when we run this code on Browser we get following output in HTML
<a href="Newwebsite">Check this Website</a>
17. What is minification, and why it is necessary ?
Minification is the process of reducing the size of script and CSS files by removing blank spaces, comments. This process removes whitespaces and comments, it will leads to minimize in file size, and thus increasing performance.
see below snippet
//simple javascript code is below
var a = 10;
a = a + 1;
a = a * 2;

//after minification we will get
var a=10;a=a+1;a=a*2;
Now with above code you can see your .js file size get reduced

18.What is Web API 's in ASP.NET MVC ?
- Web API is a new framework for building on HTTP to consuming Services over REST, it support wide range of Web browsers,desktop,mobile, smart phones

19. What is display Modes ?
with the help of Display Modes, Views selected automatically by application depending on the browser. e.g. for desktop browser application return URL as 'Views\test\Login.cshtml' and for same application mobile browsers will redirect to 'Views\test\Login.mobile.cshtml'

Summing up
We have seen very basic and frequently asked Q&A series, In next part of this Article I will cover more complex scenario's about ASP.NET MVC, till then enjoy this stuff.

Comments/Suggestions/Doubts are always welcome

Thanks
koolprasad2003