Search code examples
asp.netwebhookshttp-status-codes

How do I return a 4xx or 5xx error response in a .NET Webhook Handler?


I am currently implementing a WebHookHandler using Microsoft.AspNet.WebHooks. I have the hook running well, but I need to be able to return a 5xx or 4xx error response.

The samples only show a return in the ExecuteAsync method:

        return Task.FromResult(true);

I tried returning false in my exception handler, but the sender still sees the error as an acknowledgement and receives a 200 OK. I would like the sender to receive an error code.


Solution

  • Use the Response property of the WebHookHandlerContext argument passed to the ExecuteAsync method to set the HttpResponseMessage explicitly.