Global Action Filters in Asp.Net MVC for Exception Handling and Logging

  Total Views : 2539
  Comments : 0
 
  Keywords : Global action filters in asp.net mvc for exception / error handling, Global filters in asp.net mvc for error logging, Use of action filters in asp.net mvc with example

Generally in asp.net mvc global action filters are mainly used for expection / error handling and logging exceptions. In asp.net mvc we can apply global action filters to all action methods in our application. If we use global action filters in our asp.net mvc applications we don't need to declare action attribute on all action method explicitly.

 

We will learn global aciton filters in asp.net mvc with example let's start creating applicaiton in asp.net mvc

Create New Asp.Net MVC Application

Now let’s create a basic ASP.NET MVC 4 application to under stand View scaffolding Template for that Open visual studio studio à Go to File à Select New à Select Project

 

create new asp.net mvc project from visual studio 2012

 

After that you will see new dialog will pop up for selecting your Template and Project type. From Templates select Visual C# à inside that select Web and then project type select ASP.NET MVC 4 Web Application and here we are giving name as “MVCGlobalFilter” then finally click on OK button.

 

create new asp.net mvc project from visual studio 2012

 

Now new dialog will pop up for selecting template in that Select Internet Application Template and click ok it like as shown below

 

select internet application template in asp.net mvc application

 

After clicking on OK button a simple asp.net mvc project will be created that will be like as shown below

 

Global action filters project structure in asp.net mvc

 

Now we are going to add filter for that we need to add a Class with Name MyExceptionHandler. For adding class just right click on Filter folder select Add à Class that will be like as shown below.

 

add new class in filter folder in asp.net mvc application

Now give name for class file as MyExceptionHandler and click on Add button

 

give name to class file in asp.net mvc application

 

Once we add our MyExceptionHandler class our project strcture will be like as shown below

 

after adding filter class file in asp.net mvc application

Our class file MyExceptionHandler will contain code like as shown below 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace MVCGlobalFilter.Filters

{

public class MyExceptionHandler

{

 

}

}

After adding class we are going to create an Exception handler filter which will get called if any error is occurred in application and will display an error view page rather than displaying error with yellow page.

 

Let’s inherit ActionFilterAttribute, IExceptionFilter to MyExceptionHandler class and inside write code to handled expectation like as shown and then we will redirect to a View which we want to display when error occurs.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace MVCGlobalFilter.Filters

{

public class MyExceptionHandler : ActionFilterAttribute, IExceptionFilter

{

public void OnException(ExceptionContext filterContext)

{

Exception e = filterContext.Exception;

filterContext.ExceptionHandled = true;

filterContext.Result = new ViewResult()

{

ViewName = "SomeException"

};

}

}

}

If you check MyExceptionHandler class we given View Name as SomeException. Now Let’s add view in shared Folder with custom Message whatever we want to display. For adding View just right click on Shared folder and select Add à View and Name it as SomeException

 

adding view in asp.net mvc application shared folder

 

After selecting a new wizard will pop up for configuring view but inside that we do not require any configuration, just change View Name to SomeException and Click on Add button

 

Give name to view in asp.net mvc global filters application

 

After click on Add button an Empty View will create. Here is folder View after adding SomeException View.

 

After adding view in asp.net mvc application shared folder

 

Now open SomeException.cshtml view file that will contain code like as shown below

 

view structure in asp.net mvc global filters applicaiton

 

Now let’s add some Error Message to SomeException.cshtml View to display

 

@{

ViewBag.Title = "SomeException";

}

<h2>SomeException</h2>

<hgroup class="title">

<h1 class="error">Error.</h1>

<h2 class="error">Some thing when Wrong kindly check after few hours</h2>

</hgroup>

After adding message now let’s register global action filter in Global.asax. For that just open Global.asax file and add new function in it

 

GlobalFilters.Filters.Add(new MyExceptionHandler ());

Complete code snippet of Global.aspx file after registering global action filter in it

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Http;

using System.Web.Mvc;

using System.Web.Optimization;

using System.Web.Routing;

using MVCGlobalFilter.Filters;

namespace MVCGlobalFilter

{

// Note: For instructions on enabling IIS6 or IIS7 classic mode,

// visit http://go.microsoft.com/?LinkId=9394801

public class MvcApplication : System.Web.HttpApplication

{

protected void Application_Start()

{

AreaRegistration.RegisterAllAreas();

WebApiConfig.Register(GlobalConfiguration.Configuration);

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

RouteConfig.RegisterRoutes(RouteTable.Routes);

BundleConfig.RegisterBundles(BundleTable.Bundles);

AuthConfig.RegisterAuth();

// Calling Global action filter

GlobalFilters.Filters.Add(new MyExceptionHandler());

}

}

}

Now we will add some exception message in Home Controller for testing like as shown below

 

public class HomeController : Controller

{

public ActionResult Index()

{

ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

int value = 0;

value /= value;

return View();

}

}

Now just run application and access Home controllers Index Action Method it will get Error and it will be redirect to SomeException.cshtml View that will be like as shown below

 

output of asp.net mvc global action filters example

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User