【发布时间】:2014-10-09 12:27:38
【问题描述】:
在 ApiController 操作中,我需要在操作完成后立即关闭与数据库的连接。
在控制器下,我覆盖 OnActionExecuted 来完成此操作。
我将如何在 ApiController 操作下完成此操作?
谢谢
【问题讨论】:
标签: asp.net-web-api asp.net-apicontroller
在 ApiController 操作中,我需要在操作完成后立即关闭与数据库的连接。
在控制器下,我覆盖 OnActionExecuted 来完成此操作。
我将如何在 ApiController 操作下完成此操作?
谢谢
【问题讨论】:
标签: asp.net-web-api asp.net-apicontroller
您可以覆盖ExecuteAsync 方法:
public override Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
{
return base
.ExecuteAsync(controllerContext, cancellationToken)
.ContinueWith(t =>
{
// the controller action has finished executing,
// your custom code could come here ...
return t.Result;
});
}
【讨论】:
ContinueWith 匿名方法的 t 参数。你会在那里找到你正在寻找的东西。