Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Exception filters in MVC 5

How i can implement Exception Filters in MVC5.

I want to throw the exception to NLog and redirect the page to a default error page which displays "Something is gone wrong"

I have a Filter class as follows

using System;
using System.Diagnostics;
using System.Security.Policy;
using System.Web.Mvc;
using System.Web.Mvc.Filters;
using System.Web.Routing;
using System.Web.UI.WebControls;
using Delivros.UI.Controllers;
using Delivros.UI.Areas.User.ViewModel;
using System.Web;

namespace Delivros.UI.Filters
{

public class CustomAuthenticationFilter : IAuthenticationFilter
{
    public void OnAuthentication(AuthenticationContext filterContext)
    {

    }

    public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
    {
        Debug.WriteLine("OnAuthenticationChallenge : MyAuthenticationFilter");
    }
}
 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MyAuthorizationFilter : AuthorizeAttribute
{
     public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext.RequestContext.HttpContext.Request.Cookies[System.Configuration.ConfigurationManager.AppSettings[Convert.ToString(CookieField.cookieName)]] == null)
        {

        }
        else
{
            filterContext.Result = new RedirectToRouteResult(
            new RouteValueDictionary 
    {
                { "controller", "User" }, 
                { "action", "UserRegistration" } ,
                {"Area","User"}
            });

        }
    }
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class CustomActionFilter : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();
        base.OnResultExecuted(filterContext);
        filterContext.Result = new RedirectToRouteResult(
              new RouteValueDictionary 
            { 
                { "controller", "User" }, 
                { "action", "UserRegistration" } ,
                {"Area","User"}
            });
        // ActionResult home = new HomeController().Index();           
    }
}

public class MyResultFilter : IResultFilter
{
    public void OnResultExecuting(ResultExecutingContext filterContext)
    {

    }

    public void OnResultExecuted(ResultExecutedContext filterContext)
    {

    }
}

public class MyExceptionFilter : IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        filterContext.Result = new RedirectToRouteResult(
              new RouteValueDictionary 
            { 
                { "controller", "User" }, 
                { "action", "UserLogOut" } ,
                {"Area","User"}
            });

    }
}

}

But nothing is redirecting to the page...

like image 398
TechNo Avatar asked Jun 13 '14 09:06

TechNo


People also ask

How should you implement a new exception filter?

To register it globally, open the FilterConfig class which is present in the App_Start folder, and then modify the class as shown below. If the FilterConfig class is not already present there then just create a class file with the name FilterConfig. cs within the App_Start folder.


2 Answers

I believe it should be this code:

public class MyExceptionFilter : IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { 
            action = "UserLogOut", 
            controller = "User", 
            area = "User" 
        }));

    }

}

You may add an additional "if (!filterContext.ExceptionHandled)" statement before logging the values inside the result to make sure that the exception's unhandled for the moment.

like image 53
MerrickSoft Avatar answered Oct 12 '22 13:10

MerrickSoft


You could derive your own HandleErrorAttribute

public class NLogExceptionHandlerAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        // log error to NLog
        base.OnException(filterContext);
    }
}

Then register it globally

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new NLogExceptionHandlerAttribute());
    ...
}

By default, the HandleErrorAttribute will display the Error view located in the ~/Views/Shared folder but if you wanted to display a specific view you can set the View property of the attribute.

like image 36
James Avatar answered Oct 12 '22 11:10

James