0

Could any one tell me, How to work with Elmah in MVC 5.. when an error occurs it has to redirect to a some default page like a service error,Http not found.

public ActionResult About()
{
    ViewBag.Message = "Your app description page.";
    try
    {
        int message = Convert.ToInt32("Hello");
    }
    catch (Exception exp)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(exp);
      //  throw;
    }

    return View();
}

How could I redirect to error page

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
karthik
  • 57
  • 6
  • @Bijan Could you see what I have done... How can I redirect to specific page.When I get an exception – karthik May 15 '15 at 16:43
  • Elmah is for _logging_ not to hand-off the error to an error page. Look at either the `HandleErrorAttribute` or place something in your `Application_Error` method in your `Global.asax.cs`. – Brad Christie May 17 '15 at 17:01

1 Answers1

0

As already commented, Elmah is used basically to log error. Redirecting to error page is not Elmah's responsibility. To see the errors logged, hit the Elmah handler at

http://yourDomain.com/elmah.axd

Now, if you want to do redirect for exceptions, you can do

  1. Add <customErrors mode="On" defaultRedirect="YourErrorPage.htm"> in web.config
  2. Redirect in Application_Error of Global.asax
  3. Redirect inside a custom HandleErrorAttribute (after logging, ideally)

BUT, all these applies to unhandled exceptions. Your's is a handled exception (since throw is commented). In such cases, you have to directly redirect from the catch block, something like return Redirect("/YourErrorPage.htm");

Arghya C
  • 9,805
  • 2
  • 47
  • 66