asp.net mvc interview questions and answers


share this interview article on:

What is MVC (Model View Controller)?

MVC is an architectural pattern which separates the representation and user interaction. It’s divided into three  sections, Model, View, and Controller. Below is how each one of them handles the task.

  • The View is responsible for the look and feel.
  • Model represents the real world object and provides data to the View.
  • The Controller is responsible for taking the end user request and loading the appropriate Model and View.

 

What are the advantages of using MVC over Asp.net?

  • Provides a clean separation of concerns among UI (Presentation layer), model (Transfer objects/Domain Objects/Entities) and Business Logic (Controller).
  • Easy to UNIT Test.
  • Improved reusability of model and views. We can have multiple views which can point to the same model and vice versa.
  • Improved structuring of the code.

 

What are Html helpers in MVC?

HTML helpers help you to render HTML controls in the view. For instance if you want to display a HTML textbox on the view , below is the HTML helper code.

<%= Html.TextBox("LastName") %>

For checkbox below is the HTML helper code. In this way we have HTML helper methods for every HTML control that exists.

<%= Html.CheckBox("Married") %>

 

What is the difference between “HTML.TextBox” vs “HTML.TextBoxFor”?

Both of them provide the same HTML output, “HTML.TextBoxFor” is strongly typed while “HTML.TextBox” isn’t. Below is a simple HTML code which just creates a simple textbox with “CustomerCode” as name.

Html.TextBox("CustomerCode")

Below is “Html.TextBoxFor” code which creates HTML textbox using the property name ‘CustomerCode” from object “m”.

Html.TextBoxFor(m => m.CustomerCode)

In the same way we have for other HTML controls like for checkbox we have “Html.CheckBox” and “Html.CheckBoxFor”

 

How can we navigate from one view to another using a hyperlink?

By using the ActionLink method as shown in the below code. The below code will create a simple URL which helps to navigate to the “Home” controller and invoke the GotoHome action.

<%= Html.ActionLink("Home","Gotohome") %> 

 

What is difference between TempData and ViewData ?

“TempData” maintains data for the complete request while “ViewData” maintains data only from Controller to the view.

 

Does “TempData” preserve data in the next request also?

“TempData” is available through out for the current request and in the subsequent request it’s available depending on whether “TempData” is read or not.

So if “TempData” is once read it will not be available in the subsequent request.

 

What is the use of Keep and Peek in “TempData”?

Once “TempData” is read in the current request it’s not available in the subsequent request. If we want “TempData” to be read and also available in the subsequent request then after reading we need to call “Keep” method as shown in the code below.

@TempData["MyData"];
TempData.Keep("MyData");

The more shortcut way of achieving the same is by using “Peek”. This function helps to read as well advices MVC to maintain “TempData” for the subsequent request.

string str = TempData.Peek("Td").ToString();

 

What are partial views in MVC?

Partial view is a reusable view (like a user control) which can be embedded inside other view. For example let’s say all your pages of your site have a standard structure with left menu, header, and footer as shown in the image below.

 

For every page you would like to reuse the left menu, header, and footer controls. So you can go and create partial views for each of these items and then you call that partial view in the main view.

 

Mention what is the difference between Temp data, View, and View Bag?

In ASP.NET MVC there are three ways to pass/store data between the controllers and views.

ViewData

  1. ViewData is used to pass data from controller to view.
  2. It is derived from ViewDataDictionary class.
  3. It is available for the current request only.
  4. Requires typecasting for complex data type and checks for null values to avoid error.
  5. If redirection occurs, then its value becomes null.

ViewBag

  1. ViewBag is also used to pass data from the controller to the respective view.
  2. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0
  3. It is also available for the current request only.
  4. If redirection occurs, then its value becomes null.
  5. Doesn’t require typecasting for complex data type.

TempData

  1. TempData is derived from TempDataDictionary class
  2. TempData is used to pass data from the current request to the next request
  3. It keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action
  4. It requires typecasting for complex data type and checks for null values to avoid error. Generally, it is used to store only one time messages like the error messages and validation messages.

 

Explain what is the difference between View and Partial View?

View:

  • It contains the 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 contain the layout page.
  • Partial view does not verify for a viewstart.cshtml.We cannot put 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 any mark up.
  • We can pass a regular view to the RenderPartial method.

 

What is Routing In MVC ?

Routing is a pattern matching mechanism of incoming requests to the URL patterns which are registered in route table. Class : "UrlRoutingModule" is used for the same process.

 

What are Attribute Routing?

ASP.NET Web API supports this type routing. This is introduced in ASP.Net MVC5. In this type of routing, attributes are being used to define the routes. This type of routing gives more control over classic URI Routing. Attribute Routing can be defined at controller level or at Action level like :

[Route("{action = TestCategoryList}")] - Controller Level
[Route("customers/{TestCategoryId:int:min(10)}")] - Action Level

 

How Route Table is created ?

Method : "RegisterRoutes()" is used for registering the routes which will be added in "Application_Start()" method of global.asax file, which is fired when the application is loaded or started.

 

What are Actions in MVC?

Actions are the methods in Controller class which is responsible for returning the view or json data. Action will mainly have return type : "ActionResult" and it will be invoked from method : "InvokeAction()" called by controller.

 

What are Sections in MVC?

Section are the part of HTML which is to be rendered in layout page. In Layout page we will use the below syntax for rendering the HTML :

@RenderSection("TestSection")

And in child pages we are defining these sections as shown below :

@section TestSection{
<h1>Test Content<h1>
}

If any child page does not have this section defined then error will be thrown so to avoid that we can render the HTML like this :

@RenderSection("TestSection", required: false)

 

Can you explain RenderBody and RenderPage in ASP.Net MVC?

RenderBody is like ContentPlaceHolder in web forms. This will exist in layout page and it will render the child pages/views. Layout page will have only one RenderBody() method. RenderPage also exists in Layout page and multiple RenderPage() can be there in Layout page.

 

What is ViewStart Page in ASP.Net MVC?

This page is used to make sure common layout page will be used for multiple views. Code written in this file will be executed first when application is being loaded.

 

Explain the methods used to render the views in ASP.Net MVC?

Below are the methods used to render the views from action -

  • View() : To return the view from action.
  • PartialView() : To return the partial view from action.
  • RedirectToAction() : To Redirect to different action which can be in same controller or in different controller.
  • Redirect() : Similar to "Response.Redirect()" in webforms, used to redirect to specified URL.
  • RedirectToRoute() : Redirect to action from the specified URL but URL in the route table has been matched.

 

What are the sub types of ActionResult?

ActionResult is used to represent the action method result. Below are the subtypes of ActionResult :

  • ViewResult
  • PartialViewResult
  • RedirectToRouteResult
  • RedirectResult
  • JavascriptResult
  • JSONResult
  • FileResult
  • HTTPStatusCodeResult

 

What are Non Action methods in ASP.Net MVC?

In ASP.Net MVC all public methods have been treated as Actions. So if you are creating a method and if you do not want to use it as an action method then the method has to be decorated with "NonAction" attribute as shown below :

[NonAction]
public void TestMethod()
{
// Method logic
}

 

What is the need of Action Filters in ASP.Net MVC?

Action Filters allow us to execute the code before or after action has been executed. This can be done by decorating the action methods of controls with ASP.Net MVC attributes.

 

Mention some action filters which are used regularly in ASP.Net MVC?

Below are some action filters used :

  • Authentication
  • Authorization
  • HandleError
  • OutputCache

How can we determine action invoked from HTTP GET or HTTP POST?

This can be done in following way : Use class : "HttpRequestBase" and use the method : "HttpMethod" to determine the action request type.

 

What is Area in ASP.Net MVC?

Area is used to store the details of the modules of our project. This is really helpful for big applications, where controllers, views and models are all in main controller, view and model folders and it is very difficult to manage.

 

How we can register the Area in ASP.Net MVC?

When we have created an area make sure this will be registered in "Application_Start" event in Global.asax. Below is the code snippet where area registration is done :

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
}


Related Interview Questions

ADS

Tags: