Top ASP.Net MVC Interview Questions

1) Explain Caching ASP.NET MVC ?

-> Caching provides a way of storing frequently accessed data and reusing that data. This is an effective way of improving web application’s performance.

Advantages of caching
-> Reduce hosting server round-trips. When content is cached at the client or in proxies, it cause minimum request to server.
-> Reduce database server round-trips. When content is cached at the web server, it can eliminate the database request.
-> Reduce network traffic. When content is cached at the client side, it also reduces the network traffic.
-> Avoid time-consumption for regenerating reusable content. When reusable content is cached, it avoids the time consumption for regenerating reusable content.
-> Improve performance. Since cached content reduce round-trips, network traffic and avoid time consumption for regenerating reusable content which cause a boost in the performance.

OutputCache filter

-> You can enable output cache by adding an [OuputCache] attribute to either an individual controller action or an entire controller class.

-> By default, when you use the [OutputCache] attribute, content is cached in three locations: the web server, any proxy servers, and the web browser. You can control exactly where content is cached by modifying the Location property of the [OutputCache] attribute.

Varying the Output cache
-> In some situations, you might want different cached versions of the very same content. Imagine, for example, that you are creating a master/detail page. The master page displays a list of movie titles. When you click a title, you get details for the selected movie.
->If you cache the details page, then the details for the same movie will be displayed no matter which movie you click. The first movie selected by the first user will be displayed to all future users.
->You can fix this problem by taking advantage of the VaryByParam property of the [OutputCache] attribute. This property enables you to create different cached versions of the very same content when a form parameter or query string parameter varies.

Creating a Cache Profile
->As an alternative to configuring output cache properties by modifying properties of the [OutputCache] attribute, you can create a cache profile in the web configuration (web.config) file. Creating a cache profile in the web configuration file offers a couple of important advantages.
->First, by configuring output caching in the web configuration file, you can control how controller actions cache content in one central location. You can create one cache profile and apply the profile to several controllers or controller actions.

2) Explain Page LifeCycle of ASP.Net MVC ?

Fill Route
-> MVC requests are mapped to route tables which in turn specify which controller and action to be invoked. So if the request is the first request the thing is to fill the rout table with route collection. The filling of the route table happens in the global.asx  file.

Fetch Route
-> Depending on the URL sent “UrlRoutingModule” searches the route table to create “RouteData” object which has the details of which controller and action to invoke.

Request Context Created
-> The “RouteData” object is used to create the “RequestContext” object

Controller instance created
-> This request object is sent to “MvcHandler” instance to create the controller class instance. Once the controller class object is created it calls the “Execute” method of the controller class.

Execute Action
-> The “ControllerActionInvoker” determines which action to execute and executes the action.

Result sent
-> The action method executes and creates the type of result which can be a view result , file result , JSON result etc.

3) Explain MVC Application Execution Process ?

-> Requests to an ASP.Net MVC based web application first pass through the UrlRoutingModule object, which is an HTTP module. This module parses the request and performs route selection.
->The UrlRoutingModule object selects the first route object that matches the current object.
-> If no routes match, the UrlRoutingModule object does nothing and lets the request fall back to the regular ASP.NET or IIS request processing.
-> The UrlRoutingModule and MvcRouteHandler classes are the entry points to the ASP.NET MVC framework. They perform the following actions.

– Select the appropriate controller in and MVC application.
– Obtain a specific controller instance.
– Call the controller’s Execute method.

4) Explain Areas in MVC ?

-> Areas help you to group functionalities in to independent modules thus making the project more organized.

-> To accommodate large projects, ASP.NET MVC lets you partition Web applications into smaller units that are referred to as areas. Areas provide a way to separate a large MVC Web application into smaller functional groupings. An area is effectively an MVC structure inside an application. An application could contain several MVC structures (areas).

5) Explain Filters ?

-> Filters are custom classes that provide both a declarative and programmatic means to aid pre-action and post-action behavior to controller action methods.

Authorization Filters
-> These implement IAuthorizationFilter and make security decisions about whether to execute an action method, such as performing authentication or validating properties of the request.
-> Authorization filters run before any other filter.
-> AuthorizeAttribute, RequireHttpsAttribute

Action Filters
-> Action filters are attributes that can be applied either on a controller action method or on a controller. When applied at the controller level, they are applicable for all actions within that controller.
-> Action filters allow us to add, pre and post processing logic to an action method. They allow us to modify the way in which an action is executed.

Result Filters
-> These implement IResultFilter and wrap execution of the ActionResult object.
-> OutputCacheAttribute

Exception Filters
-> These implement IException filter and execute if there is an unhandled exception thrown during the execution of ASP.NET MVC pipline.—> e g; HandleErrorAttribute

Filters provided in ASP.NET MVC
AuthorizeAttribute.
-> Restricts access by authentication and optionally authorization.
HandleErrorAttribute.
-> Specifies how to handle an exception that is thrown by an action method.
->This filter does not catch exceptions unless the customErrors element is enabled in the Web.config file.
OutputCacheAttribute.
-> Provides output caching.
RequireHttpsAttribute.
-> Forces unsecured HTTP requests

6) Difference between View Bag, View Data and TempData ?

-> ASP.NET MVC offers us three options for passing data from controller to view and in next request.
-> In View Data, objects are accessible using strings as keys.
-> View bag allow adding properties dynamically.

Class-based (Java)Prototype-based (JavaScript)
Class and instance are distinct entities.All objects can inherit from another object.
Define a class with a class definition; instantiate a class with constructor methods.Define and create a set of objects with constructor functions.
Create a single object with the new operator.Same.
Construct an object hierarchy by using class definitions to define subclasses of existing classes.Construct an object hierarchy by assigning an object as the prototype associated with a constructor function.
Inherit properties by following the class chain.Inherit properties by following the prototype chain.
Class definition specifies all properties of all instances of a class. Cannot add properties dynamically at run time.Constructor function or prototype specifies an initial set of properties. Can add or remove properties dynamically to individual objects or to the entire set of objects.

7) What is a Partial View ?

-> A partial view is like a user control in Asp.Net web form. Partial views are reusable views like Header and Footer. It has .cshtml extension.
-> There are different ways for rendering a partial view in MVC razor.

Html.RenderPartial
-> The method result will be directly written to the HTTP response stream means it uses the same TextWriter object used in current webpage/template.
-> Faster than Partial method since it is directly written to the response stream this makes it fast.
-> Simple to use and there is no need to create any action.
-> Returns void
-> RenderPartial method is useful when the displaying data in the partial view is already in the corresponding view model.For example : In a blog to show comments of an article, we would like to use RenderPartial method since an article information with comments are already populated in the view model.

Html.Partial
-> Renders the partial view as a HTML encoded string.
-> Simple to use and there is no need to create any action.
-> The method result can be stored in a variable, since it returns string type value.

Html.RenderAction
-> The method result will be directly written to the HTTP response stream mean it uses the same TextWriter object used in current webpage/template.
-> Need to create a child action for rendering the partial view.
-> Faster than Action method method since it is directly written to the response HTTP response stream this makes it fast.
-> RenderAction method is useful when the displaying data in the partial view is independent from corresponding view model.For example : In a blog to show category list on each and every page, we would like to use RenderAction method since the list of category is populated by the different model.

Html.Action
-> Renders the partial view as an HTML string.
-> The method result can be stored in a variable, since it returns string type value.

Leave a Reply

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