Wellington Zanelli
Wellington Zanelli

Reputation: 1964

Handling error 500 in MVC4

I am having some difficulty to handle the error 500 in my MVC4 application. I've searched and found this post: How can I properly handle 404 in ASP.NET MVC?

And then I implemented this aproach: https://stackoverflow.com/a/7499406/2394172 (the long explained one)

The 404 error is ok, but I can't recover informations about the error on my other custom page when I get error 500.

I have the following code on my controller:

public ActionResult ServerError()
{
    Response.StatusCode = (int)HttpStatusCode.InternalServerError;
    Response.TrySkipIisCustomErrors = true;

    ViewBag.Error = Server.GetLastError();
    return View();
}

The Server.GetLastError() always returns null, how can I fix it or how can I recover it in other way?

Upvotes: 0

Views: 469

Answers (2)

Wellington Zanelli
Wellington Zanelli

Reputation: 1964

I solved my problem using ELMAH. The tutorial which @Pure.Krome posted on that question was very helpfull to make the custom error pages, but to log the exceptions, ELMAH saved me.

Upvotes: 0

Pure.Krome
Pure.Krome

Reputation: 87087

It's been a while since I've actually looked at my code :blush: but I'm guessing it might have something to do with this line of code.

Basically, I have to clear the error so the normal ASP.NET pipeline code doesn't throw it again. I think.

Wether that's correct code or not .. I think that is why you're GetLastError() is always empty.

Upvotes: 1

Related Questions