Does this approach use the use of ELMaH with MVC odor?

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.

+7
exception-handling asp.net-mvc elmah
source share
1 answer

I'm sure ASP.NET can handle these kinds of things for you, you don't have to call anything to redirect to your 404 action inside your global.asax. The following is an example.

 <configuration> <system.web> <customErrors mode="On"> <error statusCode="404" redirect="/servererrors/404.aspx" /> </customErrors> </system.web> </configuration> 

http://www.xefteri.com/articles/show.cfm?id=11

+2
source share

All Articles