- 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.
- Cocos2d-x游戲開發(fā):手把手教你Lua語言的編程方法
- Scala Design Patterns
- Python自動化運維快速入門
- The HTML and CSS Workshop
- JavaScript入門經(jīng)典
- RabbitMQ Cookbook
- Kotlin從基礎(chǔ)到實戰(zhàn)
- Active Directory with PowerShell
- 從零開始學(xué)C語言
- Essential C++(中文版)
- 機器學(xué)習(xí)微積分一本通(Python版)
- Training Systems Using Python Statistical Modeling
- 計算語言學(xué)導(dǎo)論
- Splunk Developer's Guide(Second Edition)
- 大學(xué)計算機基礎(chǔ)