views:

213

answers:

1

Hi folks,

I've got some custom error handling in my ASP.NET MVC site -> seems to be working fine.

I have an Error Controller, like this :-

/Controller/ErrorController.cs

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Unknown()
{
    Response.StatusCode = (int)HttpStatusCode.InternalServerError;
    return View("Error");
}

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult NotFound()
{
    Response.StatusCode = (int)HttpStatusCode.NotFound;
    return View("Error");
}

(i've also removed some other text, like the message to display, etc..)

The view is located like..

/Views/Error/Error.aspx

Now, i'm not sure how I can 'call' the Unknown method (above) when an error occurs. I thought I could use the [HandleError] attribute, but this only allows me to specify a View (and I'm don't think i'm doing that properly, cause the error url is really weird).

A: 

The [HandleError] attribute allows you to specify error pages for unhandled exceptions in your controller classes as opposed to the default custom error handling provided by ASP.NET. I'd rather use traditional ASP.NET custom error handling unless in some controllers you want to ovveride default custom error behaviour.

 <customErrors mode="On" defaultRedirect="~/Error/Unknown">
      <error statusCode="404" redirect="~/Error/PageNotFound"></error>
      <error statusCode="500" redirect="~/Error/Server"></error>
      <error statusCode="503" redirect="~/Error/Server"></error>
    </customErrors>
The_Butcher
So are you suggesting that, instead of using the [HandleError] attribute, just use the CustomErrors configuration entry? and if there's an error in the controller, then this will pick it up? (most likely controller errors == HTTP Status 500, so the ~/Error/Server route will be called?
Pure.Krome
correct :D , you made it sound so much better :)
The_Butcher
ofcourse if you want to show a specific view for a type of exception eg show the person a cool message when an ("IndexOutOfRangeException") occurs then you can use the [HandleError] attribute and this works very nicely!
The_Butcher