ASP.Net MVC Interview Questions | Page 2


Question 11:

How to define routing for an area in ASP.Net MVC?

Each area in MVC contains <AreaName>AreaRegistration file derived from AreaRegistration base class. This class had to override RegisterArea method of the base class in order to define the route for that area.

Below code snippet describes, how to define routing for the area?

public class BillManagementAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "Billing";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "BillManagement_default",
                "Billing/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }

Now this route will create routing for billing module. Url generated for this area will be looked like following

http://domainName.com/Billing/{controller}/{action}

Question 12:

What are the filters in ASP.Net MVC?

Filters are the custom classes in ASP.Net MVC that provide functionality to manage the pre-action and post-action behaviour of the controller action.

If you want to perform any operation after or before any method execution or before result displayed to the user or on exception then these filters are very useful.

There are 4 types of filters in ASP.Net MVC.

  • Authorization filters
  • Action Filters
  • Result filters
  • Exception filters
Question 13:

What is the order in which filters are executed MVC?

Below is the execution order of filters

  • Authorization filters - Authorization filter is executed before the execution of any other filters
  • Action Filters - Action filters are executed after authorization filter.
  • Result filters - Result filter executed after action filter
  • Exception filters - Exception filters executes at the last if any exception encountered.
Question 14:

Define authorization filter in MVC.

Authorization filter implements IAuthorizationFilter interface. Authorization filter runs before any other filter and responsible, to authorize a user request. ASP.Net MVC provides AuthorizeAttribute class to authorize a request.

Example and Usage:

        [Authorize]
        public ActionResult AdminDashBoard()
        {
            //To do
            return View();
        }

Any request to AdminDashBoard will only be served to the logged in user. If the user is not logged in then he/she will not be allowed. Authorize attribute can also be used at the controller level. If we define Authorize attribute at controller level the all the action method inside controller will be accessible for logged in user.

Question 15:

Describe action filters in MVC?

Action filters implement IActionFilter interface. Action filters execute before and after of an action. IActionFiler interface provides two methods

  • OnActionExcuting  - This method runs before excecution of any action
  • OnActionExcuted - This method runs after excecution of any action

Action filters are useful in perfroming logging, inspecting return value, canceling execution of the action method.


Question 16:

Describe exception filters in MVC?

Exception filters implement IExceptionFilter and execute if there is an unhandled exception thrown. ASP.Net MVC provides HandleErrorAttribute class as an exception filter.

Exception filters can be used for logging or displaying an error page. We may also define our custom exception filter.

Here is a good post about handling exception in MVC using exception filters.

Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media