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.
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.
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.
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.
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.
MVC 6 is the latest version.
Razor is simple programming syntax. We can render HTML using Razor syntax, directly write C# code in views using it.
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.
Yes, we can. Just make more than one route entry with the different key but with the same controller and action name.
@Html.TextBoxFor used for binding strongly type property with textbox while @Html.TextBox makes a simple textbox.
It exists in Global.asax file which runs on application start.
Actions are the methods exists in controller which returns the JSON data or the full or partial views.
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.
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.
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.
TempData is used to temporarily store data on key-value pair dictionary derived from TempDataDictionary class. It uses typecasting for complex data types.
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.
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”));
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.
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.
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.
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.
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.
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.
The method “@ActionLink(“Users”, “Employees”)” will generate a link with text Users to move to Employees actions located in the same controller.
The method “@RenderPartial(“TestPartialView”)” is used to render partial view in the main 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.
We can use AJAX by using two methodologies.
Exception Filters are executed at the end of every request.
Question No.27: Differentiate between custom controls and user controls?
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.
Three types of Authentication we use in Asp.Net.
Question No.29: What is the difference between View and Partial View?
There is a lot of difference between View and Partial View.
View:
Partial View:
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:
Perfect Baccarat Strategy: 15 Charts To Help An Individual Master The GameHow To Play Black…
What To Wear Into A Casino?Casino Dress Signal Acceptable AttireContentLas Vegas NightsWhat Put On To…
What To Wear Into A Casino?Casino Dress Signal Acceptable AttireContentLas Vegas NightsWhat Put On To…
Casa De Apostas Mostbet Com Apostas Esportivas OnlineSite Oficial No Brasil Apostas Esportivas Electronic Cassino…
Casa De Apostas Mostbet Com Apostas Esportivas OnlineSite Oficial No Brasil Apostas Esportivas Electronic Cassino…
Perfect Baccarat Strategy: 15 Charts To Help An Individual Master The GameHow To Play Black…