Asp Net Mvc Filters Interview Questions

In this MVC interview questions article, I have collected the most frequently asked questions which are collected after consulting with top industry experts in the field of design patterns, ASP.NET and Spring Framework. If you want to brush up with the MVC basics, which I recommend you to do before going ahead with this MVC Interview Questions, take a look at this article on MVC Architecture.

In case you came across some other questions during your interviews or have queries that might be helpful for others as well, do share them in the comment section. This MVC Interview question is divided into the following sections:

1 What is Razor in MVC?

ASP.NET MVC has always supported the concept of “view engines” – which are the pluggable modules that implement different template syntax options. The “default” view engine for ASP.NET MVC uses the same .aspx/.ascx/. master file templates as ASP.NET Web Forms. Other popular ASP.NET MVC view engines are Spart&Nhaml.

MVC 3 has introduced a new view engine called Razor.

Why is Razor?

  • Compact & Expressive.
  • Razor minimizes the number of characters and keystrokes required in a file, and enables a fast coding workflow. Unlike most template syntaxes, you do not need to interrupt your coding to explicitly denote server blocks within your HTML. The parser is smart enough to infer this from your code. This enables a really compact and expressive syntax which is clean, fast and fun to type.
  • Easy to Learn: Razor is easy to learn and enables you to quickly be productive with a minimum of effort. We can use all your existing language and HTML skills.
  • Works with any Text Editor: Razor doesnt require a specific tool and enables you to be productive in any plain old text editor (notepad works great).
  • Has great Intellisense:
  • Unit Testable: The new view engine implementation will support the ability to unit test views (without requiring a controller or web-server, and can be hosted in any unit test project – no special app-domain required).
  • Learn more here – Brief Introduction to MVC3

    4 What are the Exception filters in MVC?

    Answer

    Exception are part and parcel of an application. They are a boon and a ban for an application too. Isnt it? This would be controversial, for developers it helps them track minor and major defects in an application and sometimes they are frustrating when it lets users land on the Yellow screen of death each time. This would make the users mundane to the application. Thus to avoid this, developers handle the exceptions. But still sometimes there are a few unhandled exceptions.

    Now what is to be done for them? MVC provides us with built-in “Exception Filters” about which we will explain here.

    cc: Google

    A Yellow screen of Death can be said is as a wardrobe malfunction of our application.

    Get Started

    Exception filters run when some of the exceptions are unhandled and thrown from an invoked action. The reason for the exception can be anything and so is the source of the exception.

    Creating an Exception Filter

    Custom Exception Filters must implement the builtinIExceptionFilter interface. The interface looks as in the following,

    Whenever an unhandled exception is encountered, the OnException method gets invoked. The parameter as we can see, ExceptionContext is derived from the ControllerContext and has a number of built-in properties that can be used to get the information about the request causing the exception. Their propertys ExceptionContextpassess are shown in the following table:

    Name Type Detail
    Result ActionResult The result returned by the action being invoked.
    Exception Exception The unhandled exceptions caused from the actions in the applications.
    ExceptionHandled BOOL This is a very handy property that returns a bool value (true/false) based on if the exception is handled by any of the filters in the applicaiton or not.

    The exception being thrown from the action is detailed by the Exception property and once handled (if), then the property ExceptionHandled can be toggled, so that the other filters would know if the exception has been already handled and cancel the other filter requests to handle. The problem is that if the exceptions are not handled, then the default MVC behavior shows the dreaded yellow screen of death. To the users, that makes a very impression on the users and more importantly, it exposes the applications handy and secure information to the outside world that may have hackers and then the application gets into the road to hell. Thus, the exceptions need to be dealt with very carefully. Lets show one small custom exception filter. This filter can be stored inside the Filters folder in the web project of the solution. Lets add a file/class called CustomExceptionFilter.cs.

    Learn more here – Exception Filters in MVC

    6) What are the two ways to add constraints to a route?

    The two methods to add constraints to a route is

  • Use regular expressions
  • Use an object that implements IRouteConstraint Interface
  • 38. Filters in dotnet core | filters in mvc | mvc filters interview questions | .NET Core 3.1 MVC

    Related Posts

    Leave a Reply

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