ASP.NET MVC Interview Questions Part 5

25. What is the difference between ActionResult and ViewResult?

Answer:

In ASP.NET MVC, ActionResult and ViewResult are mainly a return type for Controller Action method.

ActionResult is an abstract class while ViewResult derives from the ActionResult class.

ViewResult can return only ViewResult type. ActionResult can return many type of Result.

If we are returning different type of view dynamically then ActionResult is best things.

Example 1: ActionResult

public ActionResult DynamicView()
{
   if (IsHtmlView)
     return View(); // returns simple ViewResult
   else
     return Json(); // returns JsonResult view
}


Example 2: ViewResult

public ViewResult Index()
  {     
      return View();
  }

26. What are the Filters in MVC?

Answer:

Filters:
ASP.NET MVC filter is a custom class where we can write our custom logic to execute before or after Action method execute. Filter can be applied to an action method or controller.

Filter provides feature to add and post action behaviors on controller's action methods.

Types of Filter:
  • Action Filters: Perform some operation before and after an action method execute and implements the IActionFilter attribute.

  • Authorization Filters: It is used to implement authentication and authorization for controller actions and implements the IAuthorizationFilter attribute.

  • Result Filters: Result filters contain logic that are executed before and after a view result is executed and implement the IResultFilter attribute.

  • Exception Filter: We use exception filter to handle errors raised by either our controller or controller action results and implements the IExceptionFilter attribute.

27. What are the ActionFilters in MVC? What are the different types of action filters?

Answer:

Action Filters are additional attributes that can be applied to either a controller section or the entire controller to modify the way in which action is executed. These attributes are special .NET classes derived from System.Attribute which can be attached to classes, methods, properties and fields.

ASP.NET MVC provides following action filters:

Output Cache: Output action filter caches the output of a controller action for a specified amount of time.

Handle Error: Handle 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.

28. What are the ways of handling an Error in MVC?

Answer:

Exception handling is the required part of the each application, whether it is web application or a Window Forms Application.

There are multiple ways to handle exception in ASP.NET MVC.

1. Using Try catch block
This is the simple ways to handle the exception handling in ASP.NET MVC. When the exception happens the catch block gets executed and it redirect to error view.

2. Override OnException method
The OnException is a void method that takes an argument as an object of ExceptionContext that has all the information about exception.

We override the "OnException" event of the controller and set the "Result" to the view. This View gets invoked when error occurs in the controller.

3. Using HandleError attribute
ASP.Net MVC has an attribute called "HandleError" that provides built-in exception filters.

We can define "HandleError" attributes as shown below.

public class HomeController : Controller
{
        [HandleError()]
        public ActionResult Index()
        {
            throw new Exception("Demo");
        }
}


4. Global Error Handling in MVC
If you want global error handling across the application, then override the "Application_Error" event and redirect the page from the global error.

29. What is Bundling and Minification in MVC?

Answer:

Bundling and Minification concept are introduced in MVC 4 and .NET framework 4.5. Both techniques are used to reduce the number of requests to the server and size of requested CSS and JavaScript, which improve page loading time.

Bundling:
Bundling helps us combine multiple JavaScript and CSS files into a single entity thus minimizing multiple requests into a single request.

Minification:
Minification is technique for removing unnecessary characters (like white space, newline, tab) and comments from the JavaScript and CSS files to reduce the size which cause improved load times of a webpage.

For example consider following JavaScript code.

Example:

sayHello = function(wish){
    //this is comment
    var msg = "Hello" + wish;
    alert(msg);
}


The above JavaScript will be optimized and minimized into following script.

sayHello=function(n){var t="Hello"+n;alert(t)}

30. What is GET and POST Actions Types?

Answer:

GET - GET is used to request data from a specified resource. With the entire GET request we pass the URL which is compulsory, however it can take the following overloads.

.get(url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] ).done/.fail

POST - POST is used to submit data to be processed to a specified resource. With all the POST requests we pass the URL (compulsory) and the data, however it can take following overloads.

.post(url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )