4

在一个ApiController动作中,我需要在动作完成后立即关闭与数据库的连接。

在控制器下,我重写OnActionExecuted以完成此操作。

我将如何在ApiController行动下完成此任务?

谢谢

4

1 回答 1

7

您可以覆盖该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;
        });
}
于 2013-07-02T14:41:45.127 回答