Skip to content

Instantly share code, notes, and snippets.

@RhysC
Created November 11, 2014 07:15
Show Gist options
  • Select an option

  • Save RhysC/0e57826d103df40e2b90 to your computer and use it in GitHub Desktop.

Select an option

Save RhysC/0e57826d103df40e2b90 to your computer and use it in GitHub Desktop.
Error Handling 404 in Asp.Net MVC 5
using System.Web.Mvc;
namespace YourWebsite.Filters
{
public class ObjectNotFoundExceptionFilterAttribute : ActionFilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception is NHibernate.ObjectNotFoundException)
{
filterContext.Result = new HttpNotFoundResult("Entity not found");
filterContext.ExceptionHandled = true;
}
}
}
}
...
<system.web>
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/Error">
<error statusCode="404" redirect="NotFound"/>
</customErrors>
<!-- other stuff here-- >
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="/NotFound" />
</httpErrors>
<!-- other stuff here-- >
</system.webServer>
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment