Exception Handling in ASP.NET            MVC       Presented by Joncash            12/3/2012                              ...
Outline• Understanding ASP.NET MVC application Error  Handling• How to Create ASP.NET MVC application Error  Handling• ASP...
Understanding ASP.NET MVC      application Error Handling• ASP.NET MVC comes with some built-in  support for exception han...
Exception Filters• Exception filters are run only if an unhandled  exception has been thrown when invoking an  action meth...
How to Create ASP.NET MVC       application Error Handling• Creating an Exception Filter  – Exception filters must impleme...
Example of Using the Built-In                  Exception Filter== controller == == controller ==                          ...
Accessing the exception details in               Error view• The HandleError filter not only just returns  the Error view ...
HandleErrorInfopublic class HandleErrorInfo{  public HandleErrorInfo(Exception exception, string       controllerName, str...
Global / local filter• Global  == global.asax.cs ==  protected void Application_Start()   {       GlobalFilters.Filters.ad...
HandleErrorAttribute Class• Namespace: System.Web.Mvc  Assembly: System.Web.Mvc (in System.Web.Mvc.dll)• You can modify th...
Returning different views for              different exceptions• We can return different views from  the HandleError filte...
HandleError• The exception type handled by this filter. It will also  handle exception types that inherit from the specifi...
13
ASP.NET MVC application ErrorHandling V.S ASP.NET application Error              HandlingHandled by ASP.NET             Ha...
ASP.NET MVC application Error        Handling with ELMAH• When we use the HandleError filter and  ELMAH in an application ...
ASP.NET MVC application Error         Handling with ELMAH• A better way to overcome this problem is  extend the HandleErro...
Example of signal ELMAH to log the               exceptionpublic class ElmahHandleErrorAttribute: HandleErrorAttribute{  p...
Question• 了解 MVC Error Handling 機制 ( 可處理的  錯誤有哪些? 如何設定 Global/Local Error  Handling ?)• 如何建立 MCV Error Handling• 如何創造 MVC ...
References• [ASP.NET MVC] Error Handling(2) – 在  Controller  裡 Handling Errors• Handling in ASP.NET MVC• Logging Errors wi...
Upcoming SlideShare
Loading in …5
×

Exception handling in asp.net

1,267 views

Published on

Answer below quesetions
Understanding MVC Error Handling ( Unhandled Exceptions ? How to register Global/ Local Error Handling ?)
How to Create MCV Error Handling
MVC Error Handling use case V.S. ASP.NET Error Handling use case
MVC Error Handling with Elmah

Published in: Technology
  • This is awesome video!! really helpful for me. Thanks for sharing with us. Following links also helped me to complete my task.

    http://www.mindstick.com/Articles/3b3db6b5-a464-4ca6-9d77-8b89e824054c/?Exception%20Handling%20in%20ASP%20NET%20MVC3

    http://www.codeproject.com/Articles/422572/Exception-Handling-in-ASP-NET-MVC
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Exception handling in asp.net

  1. 1. Exception Handling in ASP.NET MVC Presented by Joncash 12/3/2012 1
  2. 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. 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. 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. 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. 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. 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. 8. HandleErrorInfopublic class HandleErrorInfo{ public HandleErrorInfo(Exception exception, string controllerName, string actionName); public string ActionName { get; } public string ControllerName { get; } public Exception Exception { get; }} 8
  9. 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. 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. 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. 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• Doesnt catch HTTP exceptions other than 500 – The HandleError filter captures only the HTTP exceptions having status code 500 and by-passes the others. 12
  13. 13. 13
  14. 14. ASP.NET MVC application ErrorHandling V.S ASP.NET application Error HandlingHandled by ASP.NET Handled by ASP.NET MVC[HandleError] [HandleError]public ActionResult public ActionResultNotFound() InternalError(){ {throw new HttpException(404, throw new HttpException(500,"HttpException 404 "); "HttpException 500 ");} } 14
  15. 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, its 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. 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. 17. Example of signal ELMAH to log the exceptionpublic 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
  18. 18. Question• 了解 MVC Error Handling 機制 ( 可處理的 錯誤有哪些? 如何設定 Global/Local Error Handling ?)• 如何建立 MCV Error Handling• 如何創造 MVC Error Handling 與 ASP.NET Error Handling 案例• 在 MVC application 使用 Elmah 要如何正 確的記錄錯誤 18
  19. 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

×