I tried my level best to write many of the Asp.net MVC Interview questions to be asked in a software house. But all the questions cannot be covered. If anyone wants to add some question in this article, please leave a question in the comment box. I’ll add it in the article.
Question No. 1: What is MVC (Model – View – Controller)?
MVC is an architectural pattern designed for web applications which separate user interface and representation consist of three main components. The Model, the View, and the Controller.
- Model represents the Business logic and the Entities.
- View represents the User Interface
- Controller handles the URL requests, accept input data from the views, send data into models and communicate with Data Access Layer etc.
These three layers are independent of each other. i.e. If we add new Entities in Model layer, add new Views or new Controllers. These don’t affect each other at all. Hence, Testability achieved because of separation of concerns.
According to this MVC architecture pattern, User makes a request from the browser to Asp.net MVC application. The URL entered directly hit the controller. The controller decides which view should be displayed against the requested URL. It fills the required data in the model and gives to the View. Finally, view displays on the browser.
Question No. 2: What is the difference between Asp.net Webforms and Asp.net MVC?
- Asp.net Webforms
Every view page is strongly bound with the Code-behind C# file.
It has server-side controls which are bind in the code-behind file. So that’s why it is event-driven architecture model.
For each file a URL is generated, it means that there should be a physically file exist for each URL.
It has automatic state management (Session, view state etc.)
It has.ASPX extension which follows traditional Web Forms syntax.
- Asp.net MVC
In Asp.net MVC there is a separation of concern taken care of. Which means for any change in any of the three layers discussed, it doesn’t affect other.
There’s no need for actual physical page exists against each URL.
A unit test is achieved because of separation of concern.
It has Html helpers.
It has no automatic state management.
It is light-weight as compare to the Asp.net Web Forms.
It is open source.
Its pages has .CSHTML extension.
Question No.3: What is the latest version of Asp.net MVC?
MVC 6 is the latest version.
Question No.4: What is Razor Syntax?
Razor is simple programming syntax. We can render HTML using Razor syntax, directly write C# code in views using it.
Question No.5: What is meant by Routing in Asp.net MVC?
For mapping each incoming request to URL, Routing patterns are registered in Route table. It helps you to define your custom URL pattern for controllers to accept.
Question No.6: Does MVC provides us any facility to map multiple URL’s requests to a single action?
Yes, we can. Just make more than one route entry with the different key but with the same controller and action name.
Question No.7: What is the difference between @Html.TextBoxFor and @Html.TextBox?
@Html.TextBoxFor used for binding strongly type property with textbox while @Html.TextBox makes a simple textbox.
Question No. 8: Where does Application_Start method exists?
It exists in Global.asax file which runs on application start.
Question No.9: Define Actions in Asp.net MVC?
Actions are the methods exists in controller which returns the JSON data or the full or partial views.
Question No.10: Define ViewData?
ViewData has basically contained the key-value pairs as the dictionary. We can store us any type of data against a key and retrieve the original data in view by converting to same data type.
Question No.11: What is the difference between ViewBag and ViewData?
The major difference between them is ViewData requires typecasting while ViewBag used a dynamic keyword which can store original data by creating dynamic properties and need no typecasting.
Question No.12: Point out different return types of Actions in a controller?
ActionResult is the base class which can be used for child types of hierarchy as described below. There are total 9 types which can be used for returning data from an action.
- ViewResult – It is used for return a view (.cshtml file) from an action.
- PartialViewResult – It is used to return a partial view which can be displayed in other views. It is often used in AJAX requests for rendering some HTML dynamically or with using Razor Html Helper.
- JsonResult – It is used when we want to return data in JSON format.
- ContentResult – It is used to return HTTP content like text/plain.
- JavascriptResult – It is used to return Javascript code from the action which will execute in the browser.
- EmptyResult – It is used to return void (nothing) from the action.
- RedirectResult – It is used to redirect to other action even located in any different controller depending on
the URL. - RedirectToRouteResult – It is used to redirect to other action even located in any different controller.
- FileResult – It is used to return binary data from an action method.
Question No.13: Define TempData in Asp.net MVC?
TempData is used to temporarily store data on key-value pair dictionary derived from TempDataDictionary class. It uses typecasting for complex data types.
Question No.14: What is the difference between ViewData and TempData in Asp.net MVC?
The main difference is we can access ViewData from an action to its view only. While TempData can be accessed from an action to some other action or in any View. While it can also be accessed from a controller to some other controller.
Question No.15: What is the difference between Keep and Peek in TempData?
When we access TempData value, it is retained for deletion at the end of the current request. For example, when we access @TempData[“Id”]. It will be deleted and It’ll not be accessed in another request. If we want to keep the value to be accessed in another request, some other action or in some other controller. We need to use
Int userID = Convert.ToInt32(@TempData[“Id”]);
@TempData.Keep(“Id”)
We can also use some short syntax using Peek method like
Int userID = Convert.ToInt32(@TempData.Peek(“Id”));
Question No.16: Where can we find the RouteMapping code in Asp.net MVC Project?
RouteMapping code is written in RouteConfig.cs file which can be located in an App_Start folder in project route directory. It is registered using Application_Start event located in Global.asax file.
Question No.17: What do you know about attribute based routing in MVC?
Attribute-based routing used for defining custom URL structure. It is introduced in MVC 5. For example, look at the following code.
public class TestController : Controller
{
[Route(“Home/Index”)]
public ActionResult Employees()
{
return View();
}
}
In the above code Employees, action can be invoked by URL (“Home/Index”). By using attributes, you can define routes in the controller itself rather than to go to “RouteConfig.cs” file to define route or find the route in the long list of defined routes. So, it is used for the convention.
Question No.18: Can we map more than one Route to the single action?
Yes, MVC 5 provides facility to map more than one routes to a single action by using attributes.
For example, look at the following code.
public class TestController : Controller
{
[Route(“Home/Index”)]
[Route(“Home/Employees”)]
[Route(“Home/Users”)]
public ActionResult Employees()
{
return View();
}
}
So, by using the above code we can use URL’s (“Home/Index”, “Home/Employees”, “Home/Users”) to access the same action Employees.
Question No.19: What is the usage of “@Renderbody()” in Asp.net MVC?
The method “@RenderBody()” is used in the Layout page. Whenever a child page will use Layout page, “@RenderBody()” will be replaced by the code exists in child page other than sections block.
Question No.20: What is the usage of RenderSection in Asp.net MVC?
The method RenderSection is used to populate some specific code in different sections of Layout page. For example, look at the following code.
<div id=”main-section”>
<div class=”inner-area”>
@RenderBody()
</div>
</div>
@RenderSection(“Scripts”, false)
In the above code, @RenderBody() will be replaced by the child page code, and if the child page wants to add some script at the bottom of Layout page RenderSection will be used. It takes the first parameter as section name and the 2nd parameter for required. So, the child page will render Scripts data by using the following code.
@section Scripts
{
@* all code written in here will be populated at the place of @RenderSection(“Scripts”, false) found in Layout page *@
}
If the RenderSection 2nd parameter is true, then if the child page doesn’t provide code for the section compiler will give an error. If parameters is set to false, then it is not necessary to provide code for the section. It’s the developer choice.
Question No.21: What is the difference between [HttpGet] and [HttpPost] Action types?
By default, all actions take [HttpGet] request. URL entered in the browser is by default use [HttpGet] request. [HttpGet] is used to access some resource specified. While [HttpPost] is used to submit data to an action.
Question No.22: How can we navigate from one page to another in Razor View?
The method “@ActionLink(“Users”, “Employees”)” will generate a link with text Users to move to Employees actions located in the same controller.
Question No. 23: What is the usage of “@RenderPartial()” in Asp.net MVC?
The method “@RenderPartial(“TestPartialView”)” is used to render partial view in the main view.
Question No.24: How can we display Error messages in View?
We can display error messages in the view using “@Html.ValidationSummary()” in views. Or we can use “@Html.ValidationMessage(“key”)” for displaying error saved for the specific key.
Question No.25: How can we use AJAX in Asp.net MVC views?
We can use AJAX by using two methodologies.
- JQuery
- net AJAX Libraries
Question No.26: Which filter is executed at the end of every request?
Exception Filters are executed at the end of every request.
Question No.27: Differentiate between custom controls and user controls?
- Custom Controls: Custom controls are actually compiled code like DLLs. We can be easily added to the toolbox, its mean we can be easily used across multiple projects using drag and drop option. These controls are comparatively hard to create.
User Controls: User Controls (.ascx ) are just like pages (.aspx). These are comparatively easy to create as compare to user controls. At the same time tightly coupled with respect to User Interface and code. If we talk about use across multiple projects then we need to copy and paste to the other project.
Question No.28: How many types of Authentication in ASP.NET?
Three types of Authentication we use in Asp.Net.
- Windows Authentication: This authentication method uses built-in Windows security features to authenticate the user.
- Forms Authentication: Forms authentication work against a customized list of users or users in a database.
- Passport Authentication: This type of authentication use against Microsoft Passport service which is basically a centralized authentication service.
Question No.29: What is the difference between View and Partial View?
There is a lot of difference between View and Partial View.
View:
- It has a layout page.
- Before any view is rendered, viewstart page is rendered.
- View might have markup tags like body, HTML, head, title, meta etc.
- View is not lightweight as compare to Partial View.
Partial View:
- It does not have the layout page.
- The partial view does not verify for a viewstart.cshtml. We cannot put the common code for a partial view within the viewStart.cshtml.page.
- Partial view is designed specially to render within the view and just because of that it does not consist of any markup.
- We can pass a regular view to the RenderPartial method.
Question No.30: What is ViewStart?
Razor View Engine gave a new layout named which is called _ViewStart. It applied on all view automatically. Razor View Engine ( RVE ) 1st executes the _ViewStart and then start rendering the other view and merges them.
See the example of Viewstart: