The document discusses exception handling in ASP.NET MVC applications. It covers understanding MVC error handling, creating error handling using exception filters and the HandleErrorAttribute, differences between MVC and ASP.NET error handling, and using ELMAH for logging errors in MVC applications. The key points are that MVC supports exception filters for error handling, the HandleErrorAttribute can be used globally or locally, and ELMAH requires extending the HandleErrorAttribute to properly log handled exceptions.
2. Outline
• Understanding ASP.NET MVC application Error
Handling
• How to Create ASP.NET MVC application Error
Handling
• ASP.NET MVC application Error Handling V.S
ASP.NET application Error Handling
• ASP.NET MVC application Error Handling with
ELMAH
2
3. Understanding ASP.NET MVC
application Error Handling
• ASP.NET MVC comes with some built-in
support for exception handling
through exception filters.
• 4 basic Types of Filters
3
4. Exception Filters
• Exception filters are run only if an unhandled
exception has been thrown when invoking an
action method.
• The exception can come from the following
locations
– Another kind of filter (authorization, action, or result
filter)
– The action method itself
– When the action result is executed (see Chapter 12
for details on action results)
4
5. How to Create ASP.NET MVC
application Error Handling
• Creating an Exception Filter
– Exception filters must implement the
IExceptionFilter interface
• Using the Built-In Exception Filter
– HandleErrorAttribute
5
6. Example of Using the Built-In
Exception Filter
== controller ==
== controller == == View/Shared/Error.cshtml ==
== View/Shared/Error.cshtml ==
public class HomeController : :Controller
public class HomeController Controller <h1> Here:
<h1> Here:
{{ Views/Shared/Error.cshmtl</h1 >>
Views/Shared/Error.cshmtl</h1
// GET: /Home/
// GET: /Home/ <h2> Shared Error</h2 >>
<h2> Shared Error</h2
[ HandleError]
[ HandleError]
public ActionResult Index()
public ActionResult Index()
{{ == web.config ==<system.web>
== web.config ==<system.web>
throw new Exception( "Index Error" );
throw new Exception( "Index Error" );
}} <<customErrors mode =" On" ></c
customErrors mode =" On" ></c
}} ustomErrors>
ustomErrors>
</system.web>
</system.web>
6
7. Accessing the exception details in
Error view
• The HandleError filter not only just returns
the Error view but it also creates and passes
the HandleErrorInfo model to the view.
• Error View
@model System.Web.Mvc.HandleErrorInfo
<h2>Exception details</h2>
<p>
Controller: @Model.ControllerName <br>
Action: @Model.ActionName
Exception: @Model.Exception
</p>
7
8. HandleErrorInfo
public class HandleErrorInfo
{
public HandleErrorInfo(Exception exception, string
controllerName, string actionName);
public string ActionName { get; }
public string ControllerName { get; }
public Exception Exception { get; }
}
8
9. Global / local filter
• Global
== global.asax.cs ==
protected void Application_Start()
{
GlobalFilters.Filters.add(new HandleErrorAttribute());
}
• Local
[HandleError]
public ActionResult Index()
[HandleError]
public class HomeController : Controller
9
10. HandleErrorAttribute Class
• Namespace: System.Web.Mvc
Assembly: System.Web.Mvc (in System.Web.Mvc.dll)
• You can modify the default behavior of
the HandleErrorAttribute filter by setting the following properties:
– ExceptionType. Specifies the exception type or types that the filter
will handle. If this property is not specified, the filter handles all
exceptions.
– View. Specifies the name of the view to display.
• default value of Error, so by default, it renders
/Views/<currentControllerName>/Error.cshtml or
/Views/Shared/Error.cshtml.
– Master. Specifies the name of the master view to use, if any.
– Order. Specifies the order in which the filters are applied, if more than
one HandleErrorAttribute filter is possible for a method.
10
11. Returning different views for
different exceptions
• We can return different views from
the HandleError filter
[HandleError(Exception ==typeof(DbException), View =="DatabaseError")]
[HandleError(Exception typeof(DbException), View "DatabaseError")]
[HandleError(Exception ==typeof(AppException), View =="ApplicationError")]
[HandleError(Exception typeof(AppException), View "ApplicationError")]
public class ProductController
public class ProductController
{{
}}
11
12. HandleError
• The exception type handled by this filter. It will also
handle exception types that inherit from the specified
value, but will ignore all others. The default value is
System.Exception,which means that, by default, it will
handle all standard exceptions.
• When an unhandled exception of the type specified by
ExceptionType is encountered, this filter will set the
HTTP result code to 500
• Doesn't catch HTTP exceptions other than 500
– The HandleError filter captures only the HTTP exceptions
having status code 500 and by-passes the others.
12
14. ASP.NET MVC application Error
Handling V.S ASP.NET application Error
Handling
Handled by ASP.NET Handled by ASP.NET MVC
[HandleError] [HandleError]
public ActionResult public ActionResult
NotFound() InternalError()
{ {
throw new HttpException(404, throw new HttpException(500,
"HttpException 404 "); "HttpException 500 ");
} }
14
15. ASP.NET MVC application Error
Handling with ELMAH
• When we use the HandleError filter and
ELMAH in an application we will confused
seeing no exceptions are logged by ELMAH,
it's because once the exceptions are handled
by the HandleError filter it sets
theExceptionHandled property of
the ExceptionContext object to true and that
hides the errors from logged by ELMAH.
15
16. ASP.NET MVC application Error
Handling with ELMAH
• A better way to overcome this problem is
extend the HandleError filter and signal to
ELMAH
16
17. Example of signal ELMAH to log the
exception
public class ElmahHandleErrorAttribute: HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
var exceptionHandled = filterContext.ExceptionHandled;
base.OnException(filterContext);
// signal ELMAH to log the exception
if (!exceptionHandled && filterContext.ExceptionHandled)
ErrorSignal.FromCurrentContext().Raise(filterContext.Exception);
}
}
Exception Handling in ASP.NET MVC
17
19. References
• [ASP.NET MVC] Error Handling(2) – 在
Controller 裡 Handling Errors
• Handling in ASP.NET MVC
• Logging Errors with ELMAH in ASP.NET MVC 3
– Part 4 - (HandleErrorAttribute)
• Pro ASP.NET MVC 3 Framework third edition
– Adam Freeman and Steven Sanderson
• MSDN HandleErrorAttribute
19
Notas do Editor
The HandleError filter works only if the <customErrors> section is turned on in web.config.