ASP.Net MVC Interview Questions


Question 1:

How can we configure multiple URls for same Action name in ASP.Net MVC?

We can define multiple URLs in MVC by specifying multiple routes in Route.config file.

routes.MapRoute(
                name: "Route1",
                url: "{controllerName}/Aboutus",
                defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Route2",
                url: "{controllerName}/About-us",
                defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
            );

Now following URLs will be mapped to this action name

  • http://localhost:64922/Home/Aboutus
  • http://localhost:64922/Home/About-us
Question 2:

What is ASP.Net MVC?

ASP.NET MVC is a framework for developing web applications in .Net using MVC (Model -View- Controller) design. MVC is architectural design in which an application is divided among separate components (Model, view and controller) and each component is responsible for the different task.

  • Model - Represent the state and behaviour of data
  • View - Display user interface to the end user and display data
  • Controller - Responsible for handling user request, processing the request and providing the response.
Question 3:

Can we use MVC for desktop application?

No, we can not use MVC design pattern for desktop applications. MVC pattern can be used for web application. We can use MVVM (Model - view - viewModel) and MVP (Model - view - Presenter) pattern for desktop/ windows applications.

Question 4:

Describe HTML helper in ASP.Net MVC.

HTML helpers are static extension methods defined in InputExtensions class. These helper methods render HTML markup in view. ASP.Net MVC framework provides different inbuilt helper methods to render different HTML controls.

Below table describes the commonly used HTML helper methods

Name Description
@Html.Label("Name") Display Label
@Html.LabelFor(model=>model.Name) Display model binded label control
@Html.TextBox("textboxName") Display textbox control
@Html.TextBoxFor(model=>model.PropertyName) Display model binded textbox 
@Html.TextArea("name") Display Text area
@Html.CheckBox("name") Display checkbox
@Html.CheckBoxFor(model=>model.PropertyName) Display model binded checkbox
@Html.RadioButton("name") Display radio button
@Html.RadioButtonFor(model=>model.PropertyName) Display model binded radio button
@Html.Password("name") Display password field
@Html.PasswordFor(model=>model.PropertyName) Display model binded password field
@Html.Hidden("name") Display hidden field
@Html.HiddenFor(model=>model.PropertyName) Display Model binded hidden field
Question 5:

Explain request flow in ASP.Net MVC.

Below steps explain MVC request flow

  • Step 1: User requests any URL/resource.
  • Step 2: MVC routing engine parse the URL based on confogured route in routing table
  • Step 3: Invoke the matched controller action based on routing configuration.
  • Step 4: Controller's action process the user request and return response (response could be anything. it can be ActionResult or any premitive type string, int, HTTP response).
  • Step 5: Before returning response framework check for the return type of response, if retun type is view result then view engine comes in picture and render specific view.

Question 6:

Can we use the same view across multiple controllers? Explain in detail.

Yes, we can use the same view across multiple controllers.

As we know MVC provides a clear separation of concern. Each component (Model, Controller and view) have its own responsibilities and works independently.

Controller is responsible for handling and processing the user requests.

Model is responsible for holding data and representing the type of data.

Views are used to display the user interface on the browser.

So we can return any view in response on user request from any controller irrespective of where views are situated only we need to specify correct path of view so that MVC framework can locate that.

Also, we can pass the same model in different views.

 

Question 7:

How to improve the performance of page load in asp.net mvc?

ASP.Net MVC provides Minification and Bundling features to improve page load time by reducing the size of the page and reducing the number of HTTP requests made by the browser to the server in order to load the page.

System.Web.Optimization assembly is required in order to support bundling and minification.

Question 8:

How bundling is helpful in improving the performance of page load? Explain.

When a user requests any page then browser loads that page along with all dependent files (scripts, CSS files, images). In order to load a file browser have to make a new HTTP request. So Browser  makes as many requests as the number of files are attached with the page.

Generally, browsers can make only 6 simultaneous requests to get resources from the server. Additional requests are queued by the browser for later processing. Hence, if we have multiple files then it may have to wait in the request queue.

Now using bundling, we can combine multiple files in one file, hence reducing the number of files to be loaded results in faster load time.

This video demonstrates why we need bundling and minification?

Question 9:

Explain Bundling and minification.

Bundling and minification provide 2 basic functionality in order to improve the performance of page load.

Bundling - Bundle all the multiple scripts/ CSS files in one file for script bundle and one file for CSS bundle so that only browser need to load fewer files instead of multiple files.

System.Web.Optimization assembly is required in order to support bundling and minification.

BundleConfig.cs file defines the bundle for scripts and CSS. Below is sample code to create a bundle in MVC.

public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            //Script bundles
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));          

            //Style bundle
            bundles.Add(new StyleBundle("~/Content/css").Include(
                "~/Content/site.css",
                "~/Content/jquery-ui.css"
                ,"~/Content/SyntaxHighlighter.css"));
        }
    }

Minification - Minification process generates a minified file by removing comments, extra white spaces and renames the variable names. So this reduces the file size and results in the faster download.

This video explains core concepts of Bundling and Minification and how to implement bundling and minification in ASP.Net MVC.

Question 10:

What are the area in ASP.Net MVC?

ASP.Net MVC defines areas, helpful in managing a large application into smaller modules. A large application say, any e-commerce shopping portal may have different sections e.g. Men, Women, Electronic portal, billing module, reporting and order tracking module, These modules can be managed into different areas and routing for each area can be managed differently.

Example:

Below application defines 3 areas BillManagement, Order management and Men shopping portal.

Area in mvc

An area contains Controllers, Models, Views and AreaRegistration file.

Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media