ASP.Net MVC interview questions

By | September 22, 2013
ASP.Net MVC Interview Questions:
What are fundamental tenets /philosophy behind ASP.Net MVC Framework design?

  • Convention over configuration
  • Don’t repeat yourself (DRY)
  • Pluggability/extensibility wherever possible
  • Lean, clean standards based semantic html markup

Can you briefly explain new features in ASP.Net MVC 4 release?

  1. Mobile application project template to create Mobile sites. This is based on jQuery mobile Framework and adaptive rendering to improve display on mobile devices.
  2. Display Modes feature (Browser override) – uses convention based approach to allow selecting views based on browser making the requests. If a mobile browser requests view, the application returns mobile optimized view. 
  3. View Switcher (partial view and supporting controller)  which lets  us switch view display manually between Desktop and Mobile devices.
  4. TPL support for Asynchronous controllers – action methods now return an object of type Task.
  5. Integrated support for Web API framework utilizing mvc development style (Routing system) but tailored for writing Http Services.
  6. Supports OAuth and OpenID authentication/authorization with external identity providers.
  7. Bundling & Minification support for scripts and content files.
Explain the request life cycle in ASP.Net MVC site?

There are five phases which trigger when you make a request
1. RouteTable
 Of course, this steps happens only once when site starts for the first time. The RouteTable maps route definitions to route handlers.
2. UrlRoutingModule
The UrlRoutingModule intercepts every request and matches incoming request and executes the matching handler (default mvchandler)
The MvcHandler selects the controller that will handle HTTP request and passes the controller a wrapped HttpContext(ControllerContext)
4. Controller
The controller determines which controller method to execute, builds a list of parameters, and executes the method.
5. The RenderView Method is Called
Typically, a controller method calls RenderView() to render content back to the browser. The Controller.RenderView() method delegates its work to a particular ViewEngine.
Explain HTTP Methods?
Web API uses the HTTP method to select the action. 
To find the action, Web API looks at the HTTP method, and then looks for an action whose name begins with that HTTP method name. For example, with a GET request, Web API looks for an action that starts with “Get…”, such as “GetUser” or “GetAllUsers”.  This convention applies only to GET, POST, PUT, and DELETE methods.
Instead of using the naming convention for HTTP methods, you can explicitly specify the HTTP method for an action by decorating the action method with the HttpGetHttpPutHttpPost, or HttpDelete attribute.
To allow multiple HTTP methods for an action, or to allow HTTP methods other than GET, PUT, POST, and DELETE, use the AcceptVerbs attribute, which takes a list of HTTP methods.
Where are the routes for Web API controller defined?
Routes are defined in the WebApiConfig.cs file, which is placed in the App_Start directory. 
What’s default route template for Web API like?

    name: “API Default”,
    routeTemplate: “api/{controller}/{id}”,
    defaults: new { id = RouteParameter.Optional }

What is IControllerActivator?

IControllerActivator gives an opportunity to introduce Dependency Injection (DI) into controllers by creating a controller activator.  The interface contains one method called Create, which is passed a RequestContext and a Type that specifies which controller class should be instantiated.

What are the different types of Filters in MVC?
a. Authorization filter

b. Action filter

c. Result filter

d. Exception filter

What are the different return types supported by Action methods?
1. ViewResult
2. JavaScriptResult
3. RedirectResult
4. ContentResult
5. JsonResult
6. FileResult
Explain IRouteHandler interface?

Whenever a request is received by MVC, it is the job of the routing engine to match the request URL with the registered routes. After finding the matched route, the route handler for the route is being called. Each route can have its own route handler. A route handler is a class that implements IRouteHandler interface.
What is the difference between ViewData, ViewBag and TempData?

ViewData is a dictionary object and requires typecasting for complex types.
ViewBag is a dynamic property.ViewBag doesn’t have typecasting and null checks.
TempData stays for the time of an HTTP Request. It maintains data between redirects, i.e., from one controller to the other controller.
Is it possible to abort/timeout Asynchronous action method? 
Asynchronous action methods that return Task instances can support timeouts. To make your action method cancelable, add a parameter of type CancellationToken to the action method signature. 
What is non Action attribute?

To prevent a method from getting invoked as an action, use the NonAction attribute. This signals to the framework that the method is not an action, even if it would otherwise match the routing rules.

Explain order of execution for the Action filters?
Authorization filters run first before the other kinds of filter and last one to run is Exception filter

Can Partial View invoke an action method?

Explain child actions?

Child actions are action methods invoked from within a view. This lets you avoid repeating controller logic that you want to use in several places in the application. Child actions are to actions as partial views are to views.Used for creating reusable UI controls (partial views). When you use a child action, it invokes an action method, renders a view, and injects the result into the response stream.

What are sections in Razor engine?

Sections allows you to provide regions of content within a layout. Razor sections give greater control over which parts of the view are inserted into the layout and where they are placed.We define sections using the Razor @section tag followed by a name for the section.

Can we have Controller name which doesn’t end in “Controller”?

YES. The default controller factory uses “convention” around controller names when it’s trying to find a controller to dispatch the request to. You could override if we implement our custom IControllerFactory

Is it possible to have Generic controller in mvc world?

YES. You just can’t use it directly but you can inherit it and use the derived class as controller

 public class Product : Controller
        where T : new()
        { …… }
public class TestProduct : Product {


Can an ASP.Net MVC controller return an Image?

Return FilePathResult using File method of controller
Explain FileResult action result in mvc?

Represents a base class that is used to send binary file content to the response. There are four types of FileResult.
  • FileContentResult: Sends the contents of a binary file to the response.
  • FilePathResult: Sends the contents of a file to the response
  • FileResult: Returns binary output to write to the response
  • FileStreamResult: Sends binary content to the response by using a Stream instance
Can you overload controller methods in ASP.Net MVC?

YES.  Use ActionName attribute on action method.
Is the following a valid route definition?
routes.MapRoute(“”, “X{controller}/{action}”) ?
How routes in route system are applied for incoming request?

Routes are generally applied in the order in which we add them to route table.
Is the following a valid route 
routes.MapRoute(“MyRoute”, “{controller}/{action}/{id}/{*rest}”?

YES. variable length routes are valid.  This route will match any URL, irrespective of the number of placeholders it contains. The first three placeholders are used to set values for controller, action and id variables, additional placeholders are all assigned to rest variable
Can we have two controllers with the same name?

YES. we can. using namespace prioritization when attempting to resolve name of controller.

How can I ensure that only controller names beginning with “p” are available to service incoming request?

We have to define constraints by passing them as a parameter to the MapRoute method. We can define constraint with a regular expression that matches URLS only where value of controller variable with letter P
new { controller = “^P.*”,}

Can you request static files like images, html files using ASP.Net MVC architecture with out using controller and action?

YES. The routing system provides support for serving static content. It checks to see if a URL matches a disk file before evaluating the application routes;
How can you bypass specific URL from being evaluated against routes?

USE IgnoreRoute

Difference between @Html.ActionLink and @Html.RouteLink?

You can override the default routing matching behavior by using Html.RouteLink  method, which lets you specify which route you want to use.
What is areas in ASP.Net MVC?

Areas represents a functional segment of the application, such as customer, admin. This is useful in a large project. Each area has it’s own folder structure, allowing you to keep everything separate. 

Can you create outbound link to an action in a different area?

YES. Set the variable called area and use it to specify the name of the area you want
@Html.ActionLink(“Test area”, “Index”, new { area = “Test” })
How about linking an action on one of the top-level controllers from an area?

You should set the name of area as empty string.

Explain Web API routing system?

ASP.NET Web API uses the same routing system for mapping URLs to controller actions. It contextualizes the routing to HTTP services by mapping HTTP verbs to actions by convention, which both makes the code easier to read and encourages following RESTful service design.

What are Global Action filters?

Action filters which apply to all action methods in your application. Useful for application wide concerns like logging and error handling.

    Leave a Reply

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