- 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.
- Instant Node Package Manager
- Learn Type:Driven Development
- Python數(shù)據(jù)可視化:基于Bokeh的可視化繪圖
- Instant Zepto.js
- Visual Basic程序設(shè)計(jì)與應(yīng)用實(shí)踐教程
- 零基礎(chǔ)學(xué)Python網(wǎng)絡(luò)爬蟲(chóng)案例實(shí)戰(zhàn)全流程詳解(入門(mén)與提高篇)
- 軟件測(cè)試實(shí)用教程
- Spring+Spring MVC+MyBatis從零開(kāi)始學(xué)
- Java圖像處理:基于OpenCV與JVM
- FPGA嵌入式項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)
- C++程序設(shè)計(jì)教程(第2版)
- Python期貨量化交易實(shí)戰(zhàn)
- Python 3 Object:oriented Programming(Second Edition)
- OpenCV 3.0 Computer Vision with Java
- Bitcoin Essentials