0

I'm trying to understand HandleErrorAttribute in MVC3. (I also followed old article from ScottGu) I added the <customErrors mode="On" /> to the web.config file. All errors redirect to the \Views\Shared\Error.cshtml view. If I keep the HandleErrorAttribute or remove from the controller, there is no difference in the behavior. Code of the controller

public class HomeController : Controller
{
    [HandleError]
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";
        throw new Exception();
        return View();
    }


}

Also, I show in some articles and SO post, that with <error redirect="..."/>, request can be redirected to the required view.

Qestions

  1. What is the use of HandleErrorAttribute?
  2. What is the advantage of using it over <customErrors.. ?
  3. What can we achieve that is not achievable by <customErrors.. ?
tereško
  • 58,060
  • 25
  • 98
  • 150
gmail user
  • 2,753
  • 4
  • 33
  • 42

1 Answers1

0

1) The HandleErrorAttribute (MSDN) is a FilterAttribute that is used to handle controller actions that throw an error. I would suggest reading the documentation on the MSDN page as it describes exactly what it does and the constructors that it can take. Additionally in your webconfig you must have the customErrors section set to.

<system.web>
  <customErrors mode="On" defaultRedirect="Error" />
</system.web>

2) Now the custom errors section is used to allow the Asp.Net application to control the behavior of the page when an error (Exception) is raised. (MSDN) When the custom Errors is set to On or RemoteOnly when an application exception happens the application will use the rules defined in the Web.config to either display the error message or redirect to a page.

3) Using the HandleErrorAttribute you can provide different redirections \ views based on the exception types raised.

I would recommend you view this SO topic for more information (read Elijah Manor's post). ASP.NET MVC HandleError

Cheers.

Community
  • 1
  • 1
Nico
  • 12,493
  • 5
  • 42
  • 62
  • I already read that answer. That's how i got the link to Scott's article – gmail user Jan 05 '14 at 22:19
  • Great. So providing the HandleError (with the parameterless constructor) will result in the same behavior as your customErrors behaviour. The advantages are providing the error type and handlers for these handler types. – Nico Jan 05 '14 at 22:27
  • I think this is what we can't achieve just by using `customerror` – gmail user Jan 05 '14 at 22:32