1

I am new at using exceptionfilters.

Following the link: https://learn.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handling#httpresponserexception

I've created a class

public class NotImplExceptionFilterAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        if (context.Exception is NotImplementedException)
        {
            context.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);
        }
    }
}

then i used the attribute on a single method on my controller. i did not add the filter globally yet because i want it to work on a single method for now.

public class HomeController : Controller
{
    [NotImplExceptionFilter]
    public void Test()
    {
        throw new NotImplementedException("This method is not implemented");
    }
}

But the OnExepction is not being called every time i'm throwing an error. please let me know what am i missing

2 Answers 2

2

I think your problem is you are inheriting from Controller class instead ApiController as documentation says.

Since you are using System.Web.Http namespace in this attribute it works only for Api controllers.

If you want to use other controllers as well you need to add another exception filter attribute but in that case dont use System.Web.Http but use System.Web.Mvc namespace and the other part will be almost the same code(only a few small changes)

Override OnException again and make your logic in there but remember these two ways are different in how you actually show the error message to the user. First one use the response to show the message and the other one has to be implemented in a different way. Probably look at this one here

Sign up to request clarification or add additional context in comments.

4 Comments

can't do. because i have some actions on my controller that are using views and it will have error if i use apicontroller. does the exception handling only works on ApiController?
In this case it works only fot api controllers because you are currently using System.Web.Http namespace.
hmm, but is there a way that i can use error handling on a Controller not on ApiController?
@HVT_08 look at the edit. At the end there is a way how to show a error message to the user. You can make your custom way of doing this but the general idea remains the same
0

Your other problem is that you are missing the System.Web.Mvc.FilterAttribute, once you apply that, it should trigger as expected. (providing you make the change of using IExceptionFilter/ExceptionContext instead of ExceptionFilterAttribute/HttpActionExecutedContext as has already stated.

Example:

[AttributeUsage(AttributeTargets.Method]
    public class NotImplExceptionFilterAttribute : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext) {
        {
            if (filterContext.Exception is NotImplementedException)
        {
            filterContext.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.