views:

49

answers:

1

http://geekswithblogs.net/SanjayU/archive/2009/11/06/error-handling-in-asp.net-mvc-1-1-of-a-3.aspx

in this URL..

Please first two steps can anybody explain me?

The System.Web.Mvc.dll comes with the HandleErrorAttribute class, which contains..wait for it…the HandleError attribute. This information won’t be important until later in this series, but the HandleErrorAttribute class inherits from the FilterAttribute class, and implements the IExceptionFilter interface – the interface requires a method with the following signature.

public virtual void OnException(ExceptionContext filterContext);

Do i need to create Interface?

where Do I need to write OnExceptoin?

thanks

+2  A: 

You don't need to create any interface, nor worry about the OnException method or its implementation. All you need to do is decorate your controller with the [HandleError] attribute, like so:

[HandleError]
public class HomeController : Controller {}

Then go ahead and write some actions inside this controller that might throw an exception and instead of the YSOD (Yellow Screen of Death) you will see a custom error page that you've configured. You also need to activate custom errors in your web.config:

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

By default the ~/Views/Shared/Error.aspx view will be rendered in case of exception. You could define specific error views based on the exception being thrown:

[HandleError(ExceptionType = typeof(ApplicationException), View = "AppErrorPage")]

means that if an ApplicationException is thrown, the ~/Views/Shared/AppErrorPage.aspx view will get rendered.

That's what the framework provides you out-of-the-box. If this is not enough for your needs and doesn't work in your specific scenario you might start worrying about implementing a custom IExceptionFilter.

Darin Dimitrov
so what is the point of haivng OnError? method how to log the information?
Yes, logging the exception is one case where you might want to write a custom error handler and implement this method.
Darin Dimitrov
You could continue reading part 2 of this series here: http://sanjayuttam.com/wordpress/index.php/c-sharp/c-sharp-code-examples/error-handling-in-asp-net-mvc-1-part-2-of-2/
Darin Dimitrov