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

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.

主站蜘蛛池模板: 乐业县| 汉寿县| 汪清县| 从化市| 华亭县| 鸡泽县| 青海省| 通城县| 铜梁县| 孟村| 洞头县| 邹平县| 山东省| 镇雄县| 类乌齐县| 武川县| 晴隆县| 临邑县| 锦州市| 阿克| 原平市| 县级市| 当阳市| 个旧市| 百色市| 廊坊市| 宣恩县| 阿坝| 桐城市| 定远县| 寿阳县| 砀山县| 安阳县| 达拉特旗| 绥中县| 湾仔区| 仁布县| 景泰县| 元江| 河源市| 额尔古纳市|