简体   繁体   English

如何在ASP.NET MVC中创建webhook?

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

I'm trying to create a simple webhook to receive a delivery receipt from Nexmo SMS service. 我正在尝试创建一个简单的webhook来接收来自Nexmo SMS服务的送货回执。 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? 无论如何,这是在ASP.NET和端口6563上运行,所以这是我应该听的端口吗? 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. 我下载了一个名为ngrok的应用程序,它应该将我的本地Web服务器暴露给互联网,所以我运行了应用程序并指示它监听端口6563,但没有运气。 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. 通常我可以通过访问http://localhost:6563/Home/Index/CallbackURL来直接调用函数来返回视图所以我在方法签名上插入了一个断点,但是只有在我删除它时它才会被调用[HttpPost]来自它。 Any next steps that I should try? 我应该尝试的任何后续步骤?

First you have to remove the [HttpPost] bit because it clearly states that "parameters are sent via a GET". 首先,您必须删除[HttpPost]位,因为它清楚地表明“参数是通过GET发送的”。

Then you should also remove the return HttpStatusCodeResult(200) as it will return the 200 OK status code anyway if no error occures. 然后你还应该删除返回HttpStatusCodeResult(200),因为如果没有错误发生,它将返回200 OK状态代码。

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("...@...com");
        mailMessage.From = new MailAddress("..@....com");
        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 />");
    }

Before couple of weeks Asp.Net team has announced to support Web Hooks with Visual Studio. 几周之前,Asp.Net团队宣布支持使用Visual Studio的Web Hooks。

Please have a look here for more detailed information: 请查看更多详细信息:

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

Microsoft is working on ASP.NET WebHooks, a new addition to the ASP.NET family. Microsoft正在开发ASP.NET WebHooks,这是ASP.NET系列的新成员。 It supports a lightweight HTTP pattern providing a simple pub/sub model for wiring together Web APIs and SaaS services. 它支持轻量级HTTP模式,提供简单的发布/订阅模型,用于将Web API和SaaS服务连接在一起。

See Introducing Microsoft ASP.NET WebHooks Preview 请参阅Microsoft ASP.NET WebHooks预览简介

So the issue I was having wasn't with my webhook at all, it was actually with IIS Express. 所以我遇到的问题根本不在于我的webhook,实际上是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://gist.github.com/nsbingham/9548754

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM