Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

APIController "Executed" method?

In an ApiController action I need to close a connection to a database as soon as the action is finished executing.

Under a controller I override OnActionExecuted to accomplish this.

How would I accomplish this under an ApiController action?

Thanks

like image 428
Leo Avatar asked Mar 24 '23 08:03

Leo


1 Answers

You could override the ExecuteAsync method:

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;
        });
}
like image 110
Darin Dimitrov Avatar answered Apr 05 '23 12:04

Darin Dimitrov