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

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.

主站蜘蛛池模板: 都匀市| 潜江市| 罗源县| 英吉沙县| 额尔古纳市| 洪雅县| 蓬安县| 洱源县| 富锦市| 泗阳县| 独山县| 东丰县| 莱芜市| 宁波市| 鄄城县| 新邵县| 广宗县| 新乐市| 尚志市| 泗水县| 舞钢市| 政和县| 麻栗坡县| 松原市| 固阳县| 军事| 工布江达县| 易门县| 潍坊市| 永和县| 新邵县| 灵寿县| 永吉县| 诸城市| 县级市| 昌图县| 潢川县| 正定县| 客服| 专栏| 鄯善县|