官术网_书友最值得收藏!

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.

Don't forget to set ExceptionHandled to true if you don't want the exception to propagate!

The next topic is related to performance: response caching.

主站蜘蛛池模板: 翼城县| 青阳县| 长白| 南丰县| 龙海市| 孟津县| 五莲县| 库伦旗| 读书| 高淳县| 穆棱市| 两当县| 武夷山市| 灵山县| 温泉县| 瑞金市| 湾仔区| 襄樊市| 九寨沟县| 迁西县| 石河子市| 广平县| 泸定县| 桂林市| 宜黄县| 肥乡县| 江华| 沅陵县| 东丰县| 大悟县| 左云县| 长沙市| 东乌| 阳城县| 南充市| 裕民县| 玉龙| 图木舒克市| 郧西县| 博白县| 龙川县|