Simon
Simon

Reputation: 8357

MVC global exceptions

I am coding an MVC 5 internet application, and I have a question in regards to handling exceptions globally.

I have my Application_Error setup in my global.asax file. This caters to errors such as 404 HttpExceptions.

How can I send all errors that occur in a controller to the Application_Error function? An example is the following exception:

System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (name="").

I have written a OnException(ExceptionContext filterContext) for my controller, but am not sure on how to get the Application_Error function to handle these errors. Do I need to pass the exception from the OnException function, or is this the wrong approach?

Thanks in advance.

Upvotes: 1

Views: 772

Answers (2)

amiry jd
amiry jd

Reputation: 27585

I'm using some kind of http-module which gives me exactly what you are asking for:

public class MyModule : IHttpModule {

    public void Init(HttpApplication context) {
        context.Error += OnRequestError;
    }

    private void OnRequestError(object sender, EventArgs e) {
        var context = ((HttpApplication)sender).Context;

        var error = context.Error;
        if (error == null)
            return;
        var errorType = error.GetType();
        if (errorType == typeof(HttpException))
            // do something

        // this is what you are looking for
        if (errorType = typeof(HttpRequestValidationException))
            // do something, whatever you want
        // works for me, so should work to you too
    }
}

To get the module to work, you can use web.config or DynamicModuleHelper:

  • Install Microsoft.Web.Infrastructure and WebActivatorEx via nuget
  • Add a Bootstrapper class to your project
  • Register module at PreApplicationStartMethod

Sample:

// File: Bootstrapper.cs (contains class Bootstrapper)

using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using WebActivatorEx;
using WhatEver.It.Is;

[assembly: PreApplicationStartMethod(typeof(Bootstrapper), "Bootstrap")]

namespace WhatEver.It.Is {
    public class Bootstrapper {
        public static void Bootstrap() {
            // Do what do you need just before the application get started
            // like registering modules, etc...
            DynamicModuleUtility.RegisterModule(typeof(MyModule));
        }
    }
}

Upvotes: 1

Derek
Derek

Reputation: 8628

You can create a global filter by adding the following class to your App_Start folder:-

public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());

        }
    }

HandleErrorAttribute can be replaced with your own custom Exception Filter.

All you then need to do is make sure you add the following line of code to the App_Start method of your Gloabal.asax :-

 public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            //AreaRegistration.RegisterAllAreas();
            //RouteConfig.RegisterRoutes(RouteTable.Routes);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        }
    }

Hope this helps.

Upvotes: 1

Related Questions