質問

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

役に立ちましたか?

解決

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;
        });
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top