I was looking for some approaches to using ELMaH with ASP.Net MVC so that I can use the custom error page for all exceptions, including 404s.
There is no lack of questions asking how to correctly /Shared/Error.aspx work in ASP.Net MVC - either with ELMaH or without it. I did not have a problem with this task, but I feel that my decision to use a custom 404 page with ELMaH was too simple, and I cannot shake the feeling that there should be more.
After enabling customErrors in Web.Config, I created a new action in my HomeController:
public ActionResult PageNotFound() { return null; }
From there, I added a new method to my Global.asax file to take advantage of the ELMaH log filtering capabilities, and after the exception is logged, redirect the response back to the aforementioned PageNotFound ActionResult:
public void errorLog_Filtering(object sender, ExceptionFilterEventArgs e) { if (e.Exception.GetType().Equals(typeof(HttpException))) { HttpException ex = (HttpException)e.Exception; if (ex.GetHttpCode() == 404) Response.Redirect("/Home/PageNotFound"); } }
I donβt notice something that comes with MVC by default (because I still find my way to many things related to MVC), or rethinking the problem when there is a simpler solution? Appreciate any input.
Phil.Wheeler
source share