0

I am using ASP.NET MVC 4.5.1 razor C#

My Web.config is configured with one of the below tags.

<customErrors mode="On" defaultRedirect="Error">
  <error statusCode="404" redirect="/Error.htm"/>
</customErrors>

I have a class to catch all exceptions like below.

public class Error : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        base.OnException(filterContext);
    }
}

My Base Controller is like below.

[Error]
public class BaseController : AsyncController
{

}

My Controller Action Method is like below

[HttpGet, Route("All-User"), LoginAuthentication, AdminAuthorization]
public async Task<ActionResult> AllUsers()
{
    var list = await _user.List();
    return View(list);
}

My Url is like below and works perfectly

localhost/All-User

When I execute below url, I get 404 but catch block of Error class is not being executed.

localhost/All-Userabc

Am I missing something ?

I also tried to Install-Package NotFoundMvc but this is not working and getting an exception "Invalid Operation Exception"

Pankaj
  • 9,749
  • 32
  • 139
  • 283

1 Answers1

0

Your Error class won't be called for this url as it inherits HandleErrorAttribute which is called only when action method throws an exception. In your case no action method is executed as your URL does not match any route.

If you want your code to be executed for 404 errors try using default route example from this question - ASP.NET MVC - Catch All Route And Default Route

Community
  • 1
  • 1
Sergey Rybalkin
  • 3,004
  • 22
  • 26
  • Hi. I just checked the solution. It is not working on Server but only works on Development Environment. But not on IIS and not on Server. – Pankaj Sep 09 '15 at 21:11
  • Should work fine regardless of the web server and OS. But it is difficult to say what's your problem without seeing the code. – Sergey Rybalkin Sep 10 '15 at 12:27