ASP.NET MVC Interview Questions with Answers

ASP.NET MVC is the MVC based framework developed by Microsoft corporation. By using it we can easily developed and maintain large scale applications. This article describes some important questions and answers in ASP.NET MVC. Hope it will help you to build successful carrier.

What is ASP.NET?
ASP.NET is a framework for building web pages and web sites with HTML, CSS, JavaScript and server scripting. It follows object oriented programming approach. It is not a programming language. If we write code in note pad and run it we will get output. It is a server side technology. It uses all .NET compatible language such as C#, VB.NET, J# etc. If we write our code in any language after compilation .NET convert it into MSIL.

What are the Development Models in ASP.NET?
ASP.NET supports three different development models. Such:

  1. Web Pages- It is a Simplest ASP.NET model. It is Similar to PHP and classic ASP.
  2. MVC (Model View Controller)- MVC separates web applications into 3 different components: Models for data,Views for display,Controllers for input.
  3. Web Forms -The traditional ASP.NET event driven development model.

What is MVC (Model View Controller)?
The is MVC (Model View Controller) is a design pattern used in software engineering. These patterns propose there components or object to be used in software development:

  • Models – data access layer. This can be direct data access, web services, etc
  • Views – presentation layer of the application. This the users interface.
  • Controllers – business logic for the application. It controls Models & View.

What is ASP.NET MVC?
ASP.NET MVC is a web development framework developed by Microsoft Corporation. It is based on MVC (Model-View-Controller) architectural design pattern.

History of MVC
For the first time MVC Architecture was implemented by Trygve Reenskaug at 1979. It was implemented on Smalltalk at Xerox labs. The coders and software engineers accept the benefits and advantages of this architecture.

Difference between ASP.NET WebForms and ASP.NET MVC?
ASP.NET WebForms

  1. It uses Page controller pattern approach for rendering layout. That means every page has its own controller (code-behind page)
  2. Page size is big. Because it has large viewstate
  3. Testing is difficult.
  4. It is good for small scale of application and limited team size.

ASP.NET MVC

  1. It uses Front Controller approach. That means a common controller for all pages.
  2. Page size is small. Because, it has no viewstate.
  3. Testing is easy.
  4. It is good for large scale of application with large team size.

Is MVC different from a three layered architecture?
MVC is an evolution of traditional three layer architecture. Many components of the three layered architecture are part of MVC.
Look and Feel- [Three layered architecture] User interface [MVC] View
UI logic- [Three layered architecture] User interface [MVC] Controller
Business logic /validations- [Three layered architecture] Middle layer [MVC] Model
Request is first sent to- [Three layered architecture] User interface [MVC] Controller
Accessing data- [Three layered architecture] Data access layer [MVC] Model

What are the Core features of ASP.NET MVC?
Clear separation of application Presentation and Business Logic layer
An extensible and pluggable framework
Extensive support for ASP.NET Routing
Support for existing ASP.NET features

Explain the complete flow of MVC?
All clients or end user requests are first sent to the controller. Depending on the request the controller decides which model will use to fulfill the request. The controllers load the model and generate the appropriate view. The final view is sent to the end user on the browser.

Is MVC suitable for both Windows and Web applications?
The MVC architecture is suitable for a web application than Windows. For Window applications MVP (Model View Presenter) is more appropriate. If you are using WPF and Silverlight then MVVM is more suitable due to bindings.

What are the benefits of using MVC?
By using MVC we can get some benefits:
Separation of layer. If we write our code in a class file instead of code-behind page we can reuse the same code.
Page size is small. Because, it has no viewstate.
Testing is easy. Automated UI testing is possible.

How can we do authentication and authorization in MVC?
We can use Windows or Forms authentication for MVC.

What is Razor in MVC?
Razor is a light weight view engine. Before MVC 3 there was only one view type (ASPX). Razor was introduced in MVC 3.

Why Razor when we already have ASPX?
Razor is clean, lightweight, and syntaxes are easy as compared to ASPX.
For example in ASPX to display time we need to write:
<%=DateTime.Now%>
But in Razor the code is simple:
@DateTime.Now

So which is a better fit, Razor or ASPX?
As per Microsoft, Razor is more preferred. Because it’s light weight and has simple syntaxes.

How to implement AJAX in MVC?
We can implement AJAX in MVC in two ways:

What is Routing in ASP.NET MVC?
In typical ASP.NET application all the incoming requests are mapped to physical files such as .aspx file. ASP.NET MVC framework uses friendly URLs that are not mapped to physical files. It uses a routing engine that maps URLs to controller. We can define our routing rules for the engine so that it can map incoming request URLs to appropriate controller.
In a browser for an ASP.NET MVC application when a user type a URL and presses “go” button, routing engine uses routing rules that are defined in Global.asax file and find out the path of corresponding controller.

Can we create our custom view engine using MVC?
Yes.

Where is the route mapping code written?
It is written in the global.asax file.

Can we map multiple URL’s to the same action?
Yes, we can. We just need to make two entries with different key names and specify the same controller and action.

How can we navigate from one view to another using a hyperlink?
We can do it by using the ActionLink method. Here is a sample code that navigates the “Home” controller and invokes the GotoHome action.
<%= Html.ActionLink(“Home”,”Gotohome”) %>

What are ViewData, ViewBag and TempData?
ASP.NET MVC provides 3 options (ViewData, VieBag and TempData) for passing data from controller to view and in next subsequent request. Among them ViewData and ViewBag are almost similar and TempData performs additional responsibility.
ViewData and ViewBag are used to communicate between controller and corresponding view.
TempData allows for persisting information for the duration of a single subsequent request. TempData is also a dictionary object. It stays for the time of an HTTP Request. It can be used to maintain data between redirects (from one controller to the other controller).
ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
ViewData requires typecasting for complex data type and check for null values to avoid error.
ViewBag doesn’t require typecasting for complex data type.

Give an example of ViewBag & ViewData
An example of ViewBag  and ViewData is given bellow:

public ActionResult Index()
{
    ViewBag.Name = "Monjurul Habib";
    return View();
}

public ActionResult Index()
{
    ViewData["Name"] = "Monjurul Habib";
    return View();
}

In View:

@ViewBag.Name
@ViewData["Name"]

TempData Example
Sample code is given bellow:

public ActionResult Index()
{
    var model = new Review()
    {
        Body = "Start",
        Rating=5
    };
    TempData["ModelName"] = model;
    return RedirectToAction("About");
}

public ActionResult About()
{
    var model= TempData["ModelName"];
    return View(model);
}

What is the difference between ActionResult and ViewResult?
ActionResult is an abstract class. ViewResult derives from the ActionResult. ActionResult has several derived classes (ViewResult, JsonResult, FileStreamResult).
ActionResult can be used to exploit polymorphism. So if you want to return different types of views dynamically then ActionResult is the best thing.

Is MVC appropriate for both Windows and Web applications?
The MVC architecture is suitable for a web application than Windows application. For Window applications, MVP (Model View Presenter) is more applicable. For using WPF and Silverlight MVVM is more suitable.

What is the latest version of ASP.Net MVC?
The latest version of ASP.NET MVC is 6.

What is HTML helper in MVC?
The HTML helper helps us to render the HTML controls in the view. The code to display a HTML textbox on the view is given bellow:

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

How a session is maintained in MVC?
We can maintain sessions in MVC by three ways: tempdata, viewdata, and viewbag.

What is the difference between TempData and ViewData ?
TempData maintains data for the complete request while ViewData maintains data only from Controller to the view.

Does the value of TempData is available in the next request also?
The value of TempData is available in the current request and in the subsequent request. But it is depends on whether it is read or not. If it is read one time then, it will not be available in the subsequent request.

What is a partial view in MVC?

In MVC, the partial view is like a user control, a reusable view which can be used inside the other views. For example let’s say a site have a structure with right menu, header, and footer. Every page will reuse the left menu, header, and footer sections. For this condition we can create partial views for each of these sections and then call that partial view in the main view.

How we can create a partial view and use it?
During view adding time, we can create a partial view by checking the “Create partial view” check box.
After the creation we can then call the partial view in the main view using the “Html.RenderPartial” method. Sample codes for that is shown below:

<body>
<div>
<% Html.RenderPartial(“MyPartialView”); %>
</div>
</body>

How can we use validations in MVC?
In MVC the simplest way for validations is data annotations which can be applied on model properties. For example, in the below code we have a simple Customer class with a property “CustomerName”.
Here the”CustomerName” property is tagged with a Required data annotation attribute.

public class Customer
{
    [Required(ErrorMessage = "Customer name is required")]
    public string CustomerName { set; get; }
}

In Html helper class, In order to display the validation error message we need to use the ValidateMessageFor. A sample code is given bellow:

<%=Html.TextBoxFor(m => m.CustomerName)%>
<%=Html.ValidationMessageFor(m => m.CustomerName)%>

Can we display all errors in one place?
Yes, we can. For that we need to use the ValidationSummary method from the Html helper class.

In MVC how can we enable data annotation validation on client side?
We can do that by following two steps.
First step: add the reference of necessary jQuery files.

<script src="<%= Url.Content("~/Scripts/jquery-1.5.1.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/jquery.validate.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/jquery.validate.unobtrusive.js") %>" type="text/javascript"></script>

Second step:call the EnableClientValidation method.

<% Html.EnableClientValidation(); %>

For more…

Comments

  1. By McCulloch

    Reply

  2. By Cori

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *