ASP.NET
WebHooks

What are they?

in more detail...

When an event happens in a service, a notification is sent in the form of an HTTP POST request to registered subscribers.

 

The POST request contains information about the event which makes it possible for the receiver to act accordingly.

diagram...

Example...

Type a message in Slack

POST data to Endpoint

Handle that data in your application

Send response

   1: public static class WebApiConfig
   2: {
   3:     public static void Register(HttpConfiguration config)
   4:     {
   5:         // Web API configuration and services
   6:  
   7:         // Web API routes
   8:         config.MapHttpAttributeRoutes();
   9:  
  10:         config.Routes.MapHttpRoute(
  11:             name: "DefaultApi",
  12:             routeTemplate: "api/{controller}/{id}",
  13:             defaults: new { id = RouteParameter.Optional }
  14:         );
  15:  
  16:         // Load Slack receiver
  17:         config.InitializeReceiveSlackWebHooks();
  18:     }
  19: }

Receiver...

   1: public class SlackWebHookHandler : WebHookHandler
   2: {
   3:     public override Task ExecuteAsync(string generator, WebHookHandlerContext context)
   4:     {
   5:         NameValueCollection nvc;
   6:         if (context.TryGetData<NameValueCollection>(out nvc))
   7:         {
   8:             string question = nvc["subtext"];
   9:             string msg = "Sample response";
  10:             SlackResponse reply = new SlackResponse(msg);
  11:             context.Response = context.Request.CreateResponse(reply);
  12:         }
  13:         return Task.FromResult(true);
  14:     }
  15: }

Handler...

Integrations

  • Slack
  • Instagram
  • PayPal
  • Salesforce
  • Github
  • Trello
  • Wordpress
  • ...

Resources

ASP.NET WebHooks

By Thomas Morris

ASP.NET WebHooks

A quick introduction to ASP.NET WebHooks

  • 1,610