【问题标题】:Asp.net Webforms: Create webhook receiver for typeformAsp.net Webforms:为 typeform 创建 webhook 接收器
【发布时间】:2019-07-02 05:42:33
【问题描述】:

如何使用用于 Typeform 的 asp.net 网络表单制作 webhook 接收器,以及每当有人提交我的表单时我将如何在我的应用程序中获取数据。

【问题讨论】:

    标签: asp.net webforms webhooks typeform


    【解决方案1】:

    为了公开一个端点以接收 POST 请求,我将在 asp.net 世界中创建一个 HTTP 处理程序,称为“通用 Web 处理程序”,它是一个扩展名为 .ashx 的文件。

    您可以在此处查看有关如何创建的指南: https://briancaos.wordpress.com/2009/02/13/the-ashx-extension-writing-your-own-httphandler/

    实现可能如下所示:

    using System.Web;
    using Newtonsoft.Json.Linq; // From https://www.newtonsoft.com/json
    
    namespace MyNamespace
    {
      public class MyClass : IHttpHandler
      {
        public void ProcessRequest(HttpContext context)
        {
          string body = String.Empty;
          context.Request.InputStream.Position = 0;
    
          using (var inputStream = new StreamReader(context.Request.InputStream))
          {
              body = inputStream.ReadToEnd();
          }
    
          dynamic json = JObject.Parse(body);
    
          // Access the webhook payload data ie, get first answer:
          var answers = json.form_response.answers;
          Console.WriteLine(answers)
    
          context.Response.StatusCode = 200;
          context.Response.End();
        }
    
        public bool IsReusable
        {
          get { return true; }
        }
      }
    }
    

    您可以在此处找到不同 HTTP 处理程序的完整概述: https://msdn.microsoft.com/en-us/library/bb398986.aspx?f=255&MSPPError=-2147217396

    【讨论】:

    • 此代码在这一行出现错误“ var firstAnswer = payload["form_response"]["answers"][0]"==>无法应用对象类型的索引,请指导
    • @TouchStarDev 我已经更新了使用 Json.Net 的答案。这简化了 JSON 解析。您可以将其解析为一个类以避免动态使用,但您必须自己设置类模型。在此处查看示例:stackoverflow.com/a/11127428/611244
    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 2021-11-10
    • 2020-11-09
    • 1970-01-01
    • 2017-12-17
    • 2020-03-14
    • 2022-09-29
    • 2011-01-31
    相关资源
    最近更新 更多