- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 174字
- 2021-06-18 18:36:01
Error handling
In the previous chapter, we saw how to redirect to specific actions when an error occurs. Another option could be to leverage the IExceptionFilter and IAsyncExceptionFilter interfaces, one of the filter classes, to have the controller itself—or some other class—implement error handling directly.
In our controller, it's just a matter of implementing theIExceptionFilterclass, which only has one method,OnException:
public void OnException(ExceptionContext context)
{
var ex = context.Exception;
//do something with the exception
//mark it as handled, so that it does not propagate
context.ExceptionHandled = true;
}
In the asynchronous version, IAsyncExceptionFilter, the OnExceptionAsync method takes the same parameter but must return a Task.
In Chapter 10, Understanding Filters, we will learn more about the concept of filters. For now, it is enough to say that should any exception be thrown from an action in a controller implementing IExceptionFilter, its OnException method will be called.
The next topic is related to performance: response caching.
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- Python零基礎快樂學習之旅(K12實戰訓練)
- Learn WebAssembly
- C語言程序設計實踐教程
- JavaScript by Example
- PhpStorm Cookbook
- Java編程技術與項目實戰(第2版)
- Hands-On Swift 5 Microservices Development
- Learning Python Design Patterns
- Teaching with Google Classroom
- Extending Puppet(Second Edition)
- 圖數據庫實戰
- Java多線程并發體系實戰(微課視頻版)
- PHP程序設計高級教程
- 編程風格:程序設計與系統構建的藝術(原書第2版)