ASP.NET MVC, WEB API Questions and answers.

Explain MVC (Model-View-Controller)

MVC (Model-View-Controller) is an architectural software pattern that basically decouples various​​ components of a web application. By using MVC pattern, we can develop applications that are more flexible to changes without affecting the other components of our application.

  • "Model" is basically domain data.

  • "View" is user interface to render domain data.

  • "Controller" translates user actions into appropriate operations performed on model.


ASP.NET MVC is a web development framework from Microsoft that is based on MVC (Model-View-Controller) architectural design pattern. Microsoft has streamlined the development of MVC based applications using ASP.NET MVC framework.

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

ASP.NET Web Forms uses Page controller pattern approach for rendering layout, whereas ASP.NET MVC uses Front controller approach. In case of Page controller approach, every page has its own controller, i.e., code-behind file that processes the request. On the other hand, in ASP.NET MVC, a common controller for all pages processes the requests.

What are the Core features of ASP.NET MVC?

Core features of ASP.NET MVC framework are:

Clear separation of application concerns (Presentation and Business Logic)

An extensible and pluggable framework

Extensive support for ASP.NET Routing

Support for existing ASP.NET features



Can you please explain the request flow in ASP.NET MVC framework?

What is Routing in ASP.NET MVC?

In case of a typical ASP.NET application, incoming requests are mapped to physical files such as .aspx file. ASP.NET MVC framework uses​​ friendly URLs that more easily describe user’s action but are not mapped to physical files.

ASP.NET MVC framework uses a routing engine, that maps URLs to controller classes. We can define routing rules for the engine, so that it can map incoming request URLs to appropriate controller.

Practically, when a user types a URL in a browser window for an ASP.NET MVC application and presses “go” button, routing engine uses routing rules that are defined in Global.asax file in order to parse the URL and find out the path of corresponding controller.

public​​ class​​ RouteConfig

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ static​​ void​​ RegisterRoutes(RouteCollection​​ routes)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ routes.MapRoute(

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ name:​​ "Default",

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ url:​​ "{controller}/{action}/{id}",

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ defaults:​​ new​​ { controller =​​ "Home", action =​​ "Index", id =​​ UrlParameter.Optional }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ );

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ }

What is the difference between ViewData, ViewBag and TempData?

In order to pass data from controller to view and in next subsequent request, ASP.NET MVC framework provides different options i.e., ViewData, ViewBag and TempData.

Both ViewBag and ViewData are used to communicate between controller and corresponding view. But this communication is only for server call, it becomes null if redirect occurs. So, in short, it's a mechanism to maintain state between controller and corresponding view.

ViewData is a dictionary object while ViewBag is a dynamic property (a new C#​​ 4.0 feature). ViewDatabeing a dictionary object is accessible using strings as keys and also requires typecasting for complex types. On the other hand, ViewBag doesn't have typecasting and null checks.

TempData is also a dictionary object that stays for the time of an HTTP Request. So, Tempdata can be used to maintain data between redirects, i.e., from one controller to the other controller.

What are Action Methods in ASP.NET MVC?

The ASP.NET MVC framework maps URLs to classes that are referred to as​​ controllers. Controllers process incoming requests, handle user input and interactions, and execute appropriate application logic. A controller class typically calls a separate view component to generate the HTML markup for the request.

The base class for​​ all controllers is the​​ ControllerBase​​ class, which provides general MVC handling. The​​ Controller​​ class inherits from​​ ControllerBase​​ and is the default implement of a controller. The​​ Controller​​ class is responsible for the following processing stages:

  • Locating the appropriate action method to call and validating that it can be called.

  • Getting the values to use as the action method's arguments.

  • Handling all errors that might occur during the execution of the action method.

  • Providing the default​​ WebFormViewEngine​​ class for rendering ASP.NET page types (views).



public​​ class​​ HomeController : Controller


 ​​ ​​ ​​​​ public​​ ActionResult Index()

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ ViewData["Message"] =​​ "Welcome to ASP.NET MVC!";


 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return​​ View();

 ​​ ​​ ​​​​ }


 ​​ ​​ ​​​​ public​​ ActionResult About()

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return​​ View();

 ​​ ​​ ​​​​ }




Explain the role of Model in ASP.NET MVC?

One of the core features of ASP.NET MVC is that it separates the input and UI logic from​​ business logic. Role of Model in ASP.NET MVC is to contain all application logic including validation, business and data access logic except view, i.e., input and controller, i.e., UI logic.

Model is normally responsible for accessing data from some persistent medium like database and manipulate it.

What are Action Filters in ASP.NET MVC?

If we need to apply some specific logic before or after action methods, we use action filters. We can apply these action filters to a controller or a specific controller action. Action filters are basically custom classes that provide a means for adding pre-action or post-action behavior to controller actions.

For example:

  • Authorize filter can be used to restrict access to a specific user or a role.

  • OutputCache filter can cache the output of a controller action for a specific duration.




Unobtrusive JavaScript is a general term that conveys a general philosophy, similar to the term REST (Representational State Transfer). The high-level description is that unobtrusive JavaScript doesn't intermix JavaScript code in your page markup. For example, rather than hooking in via event attributes like onclick and onsubmit, the unobtrusive JavaScript attaches to elements by their ID or class, often based on the presence of other attributes (such as HTML5 data-attributes).

It's got semantic meaning and all of it; the tag structure, element attributes and so on should have a precise meaning. Strewing JavaScript gunk across the page to facilitate interaction (I'm looking at you, -doPostBack!) harms the content of the document.

What is JSON Binding?

MVC 3 included JavaScript Object Notation (JSON) binding support via the new JsonValueProviderFactory, enabling the action methods to accept and model-bind data in JSON format. This is especially useful in advanced Ajax scenarios like client templates and data binding that need to post data back to the server.

What is Dependency Resolution?

MVC 3 introduced a new concept called a dependency resolver, that greatly simplified the use of dependency injection in your applications. This made it easier to decouple application components, making them more configurable and easier to test.

Support was added​​ for the following scenarios:

Controllers (registering and injecting controller factories, injecting controllers)

Views (registering and injecting view engines, injecting dependencies into view pages)

Action filters (locating and injecting filters)

Model​​ binders (registering and injecting)

Model validation providers (registering and injecting)

Model metadata providers (registering and injecting)

Value providers (registering and injecting)


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.


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”.

What is routing in MVC?

Routing helps you to define a URL structure and map the URL with the controller.

For instance let’s say we want that when a user types “http://localhost/View/ViewCustomer/”, it goes to the “Customer” Controller and invokes the DisplayCustomer action. This is defined by adding an entry in to the routes collection using the maproute function. Below is the underlined code which shows how the URL structure and mapping with controller and action is defined.


 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ "View",​​ // Route name

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ "View/ViewCustomer/{id}",​​ // URL with parameters

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ new​​ { controller =​​ "Customer", action =​​ "DisplayCustomer",​​ 

id = UrlParameter.Optional });​​ // Parameter defaults  ​​​​ 

Where is the route mapping code written?

The route mapping code is written in "RouteConfig.cs" file and registered using "global.asax" application start event.

Can we map multiple URL’s to the same action?

Yes, you can, you just need to make two entries with different key names and specify the same controller and action.

Explain attribute based routing in MVC?

This is a feature introduced in MVC 5. By using the "Route" attribute we can define the URL structure. For example in the below code we have decorated the "GotoAbout" action with the route attribute. The route attribute says that the "GotoAbout" can be invoked using the URL structure "Users/about".

public class HomeController : Controller


 ​​ ​​ ​​ ​​ ​​ ​​​​ [Route("Users/about")]

 ​​ ​​ ​​ ​​ ​​ ​​​​ public ActionResult GotoAbout()

 ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return View();

 ​​ ​​ ​​ ​​ ​​ ​​​​ }


What is the advantage of defining route structures in the code?

Most of the time developers code in the action methods. Developers can see the URL structure right upfront rather than going to the “routeconfig.cs” and see the lengthy codes. For instance in the below code the developer can see right upfront that the “GotoAbout” action can be invoked by four different URL structure.

This is much user friendly as compared to scrolling through the “routeconfig.cs” file and going through the length line of code to figure out which URL structure is mapped to which action.

public class HomeController : Controller


 ​​ ​​ ​​ ​​ ​​ ​​​​ [Route("Users/about")]

 ​​ ​​ ​​ ​​ ​​ ​​​​ [Route("Users/WhoareWe")]

 ​​ ​​ ​​ ​​ ​​ ​​​​ [Route("Users/OurTeam")]

 ​​ ​​ ​​ ​​ ​​ ​​​​ [Route("Users/aboutCompany")]

 ​​ ​​ ​​ ​​ ​​ ​​​​ public ActionResult GotoAbout()

 ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return View();

 ​​ ​​ ​​ ​​ ​​ ​​​​ }


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")​​ %> ​​​​ 

How can we restrict MVC actions to be invoked only by GET or POST?

We can decorate the MVC action with the HttpGet or HttpPost attribute to restrict the type of HTTP calls. For instance you can see in the below code snippet the DisplayCustomer action can only be invoked by HttpGet. If we try to make HTTP POST on DisplayCustomer, it will throw an error.

​​ [HttpGet]

public​​ ViewResult DisplayCustomer(int​​ id)


 ​​ ​​ ​​​​ Customer objCustomer = Customers[id];

 ​​ ​​ ​​​​ return​​ View("DisplayCustomer",objCustomer);


How can we maintain sessions in MVC?

Sessions can be maintained in MVC by three ways: tempdata, viewdata, and viewbag.

What is the difference between tempdata, viewdata, and viewbag?

Figure: Difference between tempdata, viewdata, and viewbag

Temp data - Helps to maintain data when you move from one controller to another controller or from one action to another action. In other words when you redirect, tempdata helps to maintain data between those redirects. It internally uses session variables.

View data - Helps to maintain data when you move from controller to view.

View Bag - It’s a dynamic wrapper around view data. When you use Viewbag type, casting is not required. It uses the dynamic keyword internally.

Figure: dynamic keyword

Session variables - By using session variables we can maintain data from any entity to any entity.

Hidden fields and HTML controls - Helps to maintain data from UI to controller only. So you can send data from HTML controls or hidden fields to the controller using POST or GET HTTP methods.

Below is a summary table which shows the different mechanisms for persistence.

Maintains data between



Hidden fields


Controller to Controller





Controller to View





View to Controller





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.



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();





Recommended Book for MVC : List of Some Important books for .NET Framework, C#, MVC, WCF


(Visited 943 times, 1 visits today)

Leave a Reply

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