ASP.NET MVC Interview Questions and Answers for Freshers, Experienced


ASP.Net MVC is a pattern which is used to split the application's implementation logic into three components i.e. models, views, and controllers.

  • • The business layer (Model logic)
  • • The display layer (View logic)
  • • The input control (Controller logic)

The Model It represents the application data domain. In other words applications business logic is contained within the model and is responsible for maintaining data.

Often model objects retrieve data (and store data) from a database.

The View is the part of the application that handles the display of the data.

It is user interface to render domain data.

The Controller is the part of the application that handles user interaction.

Below are some of the features supported in C# -

  • • Mobile templates
  • • Separation of application concerns
  • • Added ASP Dot NET Web API template for creating REST based services.
  • • It supports for Test Driven Development (TDD) approach
  • • Asynchronous controller task support.
  • • Bundling the java scripts.
  • • Support for existing ASP.NET features like membership and roles, authentication and authorization, provider model and caching etc.
  • • Segregating the configs for MVC routing, Web API, Bundle etc
  • • Receive first request for the application
  • • Performs routing
  • • Creates MVC request handler
  • • Create Controller
  • • Execute Controller
  • • Invoke action
  • • Execute Result

Layout pages, can define sections, which can then be overriden by specific views making use of the layout. Defining and overriding sections is optional.

The base type of all these result types is ActionResult.

1. ViewResult (View): This return type is used to return a webpage from an action method.

2. PartialviewResult (Partialview): This return type is used to send a part of a view which will be rendered in another view.

3. RedirectResult (Redirect): This return type is used to redirect to any other controller and action method depending on the URL.

4. RedirectToRouteResult (RedirectToAction, RedirectToRoute): This return type is used when we want to redirect to any other action method.

5. ContentResult (Content): This return type is used to return HTTP content type like text/plain as the result of the action.

6. jsonResult (json): This return type is used when we want to return a JSON message.

7. javascriptResult (javascript): This return type is used to return JavaScript code that will run in browser.

8. FileResult (File): This return type is used to send binary output in response.

9. EmptyResult: This return type is used to return nothing (void) in the result.

  • • Razor Engine is an advanced view engine that was introduced with MVC3. This is not a new language but it is a new markup syntax.
  • • The file extensions used with Razor Engine are different from Web Form Engine. It has .cshtml (Razor with C#) or .vbhtml (Razor with VB) extension for views, partial views, editor templates and for layout pages.
  • • By default, Razor Engine prevents XSS attacks(Cross-Site Scripting Attacks) means it encodes the script or html tags like <,> before rendering to view.

Razor syntax are easy to learn and much clean than Web Form syntax. Razor uses @ symbol to make the code like as:

Filters allow us to add pre-action and post-action behaviors from action, for achieving this functionality ASP.NET MVC provides a feature called Filters.

ASP.NET MVC supports following types of filters:

  • 1. Action Filters : Action Filters allow us to add before-action and after-action behavior to controller action methods.
ASP.NET MVC provides the following action filters:
  • Output Cache: This action filter caches the output of a controller action for a specified amount of time.
  • Handle Error: This action filter handles errors raised when a controller action executes.
  • Authorize: This action filter enables you to restrict access to a particular user or role.
  • 2. Authorization Filters :- It is used to implement authorization and authentication for action filters
  • 3. Result Filters :- Performs some operation before or after the execution of view result.
  • 4. Exception Filters :- Performs some operation if there is an unhandled exception thrown during the execution of the ASP.NET MVC pipeline.
  • Layout pages are similar to master pages in traditional web forms. This is used to set the common look across multiple pages.

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.

This method is used to render the specified partial view as an HTML string. This method does not depend on any action methods. We can use this like below :-

"RouteConfig.cs" holds the routing configuration for MVC. RouteConfig will be initialized on Application_Start event registered in Global.asax.

In an ASP.NET web application that does not make use of routing, an incoming browser request should map to a physical file. If the file does not exist, we get page not found error.

An ASP.NET web application that does make use of routing, makes use of URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.

What are the 3 things that are needed to specify a route ?
  • 1. URL Pattern - You can include placeholders in a URL pattern so that variable data can be passed to the request handler without requiring a query string.
  • 2. Handler - The handler can be a physical file such as an .aspx file or a controller class.
  • 3. Name for the Route - Name is optional.

routes.MapRoute(

"View", // Route name

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

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

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

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 = CourseList}")] - Controller Level

[Route("Students/{CourseListId:int:min(10)}")] - Action Level

  • 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 "SubjectCode" as name.
Html.TextBox("SubjectCode")
  • Below is "Html.TextBoxFor" code which creates HTML textbox using the property name "SubjectCode" from object "m".
  • Using the typed TextBoxFor version will allow you to use compile time checking. So if you change your model then you can check whether there are any errors in your views.
Html.TextBoxFor(m => m.SubjectCode)

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

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.
  • "BundleConfig.cs" in ASP.Net MVC4 is used to register the bundles by the bundling and minification system. Many bundles are added by default including jQuery libraries like - jquery.validate, Modernizr, and default CSS references.

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(

"~/Scripts/jquery-3.1.1.min.js",

"~/Scripts/jquery.validate.min.js",

"~/Scripts/jquery.validate.unobtrusive.min.js"));

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

AJAX Helpers are used to create AJAX enabled elements like as Ajax enabled forms and links which performs the request asynchronously and these are extension methods of AJAXHelper class which exists in namespace - System.Web.ASP.Net MVC.

  • Session can be maintained in MVC by three ways tempdata, viewdata, and viewbag.

Partial view in MVC renders a portion of view content. It is helpful in reducing code duplication. In simple terms, partial view allows to render a view within the parent view.

Scaffolding in ASP.NET MVC is used to generate the Controllers,Model and Views for create, read, update, and delete (CRUD) functionality in an application. The scaffolding will be knowing the naming conventions used for models and controllers and views.

Below are the types of scaffoldings –

  • • Empty
  • • Create
  • • Delete
  • • Details
  • • Edit
  • • List

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.

REST is an architectural style which uses HTTP protocol methods like GET, POST, PUT, and DELETE to access the data. MVC works in this style. In MVC 4 there is a support for Web API which uses to build the service using HTTP verbs.

Below are the options in AJAX helpers –

  • Url – This is the request URL.
  • Confirm – This is used to specify the message which is to be displayed in confirm box.
  • OnBegin – Javascript method name to be given here and this will be called before the AJAX request.
  • OnComplete – Javascript method name to be given here and this will be called at the end of AJAX request.
  • OnSuccess - Javascript method name to be given here and this will be called when AJAX request is successful.
  • OnFailure - Javascript method name to be given here and this will be called when AJAX request is failed.
  • UpdateTargetId – Target element which is populated from the action returning HTML.

Stay Connected

Popular Posts

Get Latest Stuff Through Email


Who Should Read TechTrick?

All the tricks and tips that TechTrick provides only for educational purpose. If you choose to use the information in TechTrick to break into computer systems maliciously and without authorization, you are on your own. Neither I (TechTrick Admin) nor anyone else associated with TechTrick shall be liable. We are not responsibe for any issues that caused due to informations provided here. So, Try yourself and see the results. You are not losing anything by trying... We are humans, Mistakes are quite natural. Here on TechTrick also have many mistakes..