Reputation: 2765
I'm trying to handle eventual errors in my view, by using the HandleError attribute on my view:
The reason why the Action is called 'Error' is because it gets a list of logged errors from a database.
[HandleError]
public ActionResult Error(int? page)
{
var errors = errorRepository.GetErrors();
// stuff for paging
var pageSize = 10;
var pageNumber = (page ?? 1); // if there is no page, return page 1
return View("Error", errors.ToPagedList(pageNumber, pageSize));
}
This is the error page in the /Shared/ folder:
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>
But for some reason, the error page is never being shown, even though I've forced an exception in the action method. It just goes to the default url in my RouteConfig file.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Any hint as to why it doesn't show my error page is greatly appreciated!
Upvotes: 1
Views: 204
Reputation: 331
I am sorry I have to add this as answer, but I don't have enough points to comment.
To be able to help you I need to see the code within the HandleErrorAttribute. However what you normally want to do in these cases is:
1) Add a config setting in the web.config to say that you will handle the exceptions on your own. Something like:
<system.web>
<customErrors mode="On" defaultRedirect="~/Error">
<error statusCode="500" redirect="~/Error/InternalServer" />
<error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>
</system.web>
2) Add the methods to accept those incoming calls in the ErrorController (In this case Index(), InternalServer(), NotFound())
3) Get the logs from your database and display them to the user than
Upvotes: 2