APIController"执行"方法?

Leo*_*Leo 4 asp.net-web-api asp.net-apicontroller

ApiController动作中,我需要在动作执行完毕后立即关闭与数据库的连接.

在控制器下我覆盖OnActionExecuted以实现此目的.

如何在ApiController行动中实现这一目标?

谢谢

Dar*_*rov 6

您可以覆盖该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;
        });
}
Run Code Online (Sandbox Code Playgroud)