How do I create a webhook in ASP.NET MVC?

18,299

Solution 1

So the issue I was having wasn't with my webhook at all, it was actually with IIS Express. Apparently it blocks most traffic from foreign hosts so there is some tweaking you can do before tunneling anything to your server. If you follow these guides you should have a working server.

https://gist.github.com/nsbingham/9548754

https://www.twilio.com/blog/2014/03/configure-windows-for-local-webhook-testing-using-ngrok.html

Solution 2

First you have to remove the [HttpPost] bit because it clearly states that "parameters are sent via a GET".

Then you should also remove the return HttpStatusCodeResult(200) as it will return the 200 OK status code anyway if no error occures.

Then you should simply read the values from querystring or using model binding. Here is a sample:

    public string CallbackURL()
    {
        string vals = "";

        // get all the sent data 
        foreach (String key in Request.QueryString.AllKeys)
            vals += key + ": " + Request.QueryString[key] + Environment.NewLine;

        // send all received data to email or use other logging mechanism
        // make sure you have the host correctly setup in web.config
        SmtpClient smptClient = new SmtpClient();
        MailMessage mailMessage = new MailMessage();
        mailMessage.To.Add("[email protected]");
        mailMessage.From = new MailAddress("[email protected]");
        mailMessage.Subject = "callback received";
        mailMessage.Body = "Received data: " + Environment.NewLine + vals;
        mailMessage.IsBodyHtml = false;
        smptClient.Send(mailMessage);

        // TODO: process data (save to database?)

        // disaplay the data (for degugging purposes only - to be removed)
        return vals.Replace(Environment.NewLine, "<br />");
    }

Solution 3

Before couple of weeks Asp.Net team has announced to support Web Hooks with Visual Studio.

Please have a look here for more detailed information:

https://neelbhatt40.wordpress.com/2015/10/14/webhooks-in-asp-net-a-visual-studio-extension/

Solution 4

Microsoft is working on ASP.NET WebHooks, a new addition to the ASP.NET family. It supports a lightweight HTTP pattern providing a simple pub/sub model for wiring together Web APIs and SaaS services.

See Introducing Microsoft ASP.NET WebHooks Preview

Share:
18,299
Dillon Drobena
Author by

Dillon Drobena

Updated on June 15, 2022

Comments

  • Dillon Drobena
    Dillon Drobena almost 2 years

    I'm trying to create a simple webhook to receive a delivery receipt from Nexmo SMS service. The only documentation on their website was this.

    During account set-up, you will be asked to supply Nexmo a CallBack URL for Delivery Receipt to which we will send a delivery receipt for each of your SMS submissions. This will confirm whether your message reached the recipient's handset. The request parameters are sent via a GET (default) to your Callback URL and Nexmo will be expecting response 200 OK response, or it will keep retrying until the Delivery Receipt expires (up to 72 hours).

    I've been searching for ways to do this, and so far I have this method from an example I found online, although I'm not sure if this is correct. Anyways, this is being run on ASP.NET and on port 6563, so is this the port I'm supposed to be listening to? I downloaded an application called ngrok which should expose my local web server to the internet, so I ran the application and instructed it to listen onto port 6563, but no luck. I've been fiddling with it trying to find someway to post to this function.

    [HttpPost]
    public ActionResult CallbackURL()
    {
        System.IO.StreamReader reader = new System.IO.StreamReader(HttpContext.Request.InputStream);
        string rawSendGridJSON = reader.ReadToEnd();
        return new HttpStatusCodeResult(200);
    }
    

    Usually I can call the function directly to return the view just by visiting http://localhost:6563/Home/Index/CallbackURL So I've inserted a breakpoint on the method signature, but it'll only get called if I remove the [HttpPost] from it. Any next steps that I should try?