如何制作一个“ webhook”?

eus*_*taf 0 c# asp.net asp.net-webhooks

我需要“ WinForm”应用程序来进行通信。
 “ Webhook”计划从viber接收数据(事件),然后该数据将在应用程序“ WinForm”中使用。

我做了:

  1. 创建项目“ ASP.NET Web应用程序(.NET Framework)”;
  2. 选择一个模板-“空” +“ MVC” +“ API”;
  3. 添加了控制器“控制器MVC 5-空”。控制器名称“ HookController”;
  4. 我运行应用程序“邮递员”;
  5. “邮差”。我将请求设置为“ POST”;
  6. “邮差”。我设置链接http://localhost:44836/Hook;
  7. “邮差”。点击“发送”;
  8. 结果见图片“-= RESULT =-”;

如果我正确理解了该理论,则在执行之后"Postman" action. I click "SEND",该ViberProcess (HttpContext context)方法应在HookController.cs控制器中执行,并且代码应在处停止breakpoint
这没有发生。

文档Viber REST API- 链接

题。
如何制作Webhook?

HookController.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using System.Web;
using System.Web.Mvc;

//
using System.Runtime.Remoting.Contexts;

namespace WebAppl1.Controllers
{
    public class HookController : Controller
    {
        // GET: Hook
        //public ActionResult Index()
        //{
        //    return View();
        //}

         [HttpPost]
        // [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void ViberProcess(HttpContext context)
        {
            try
            {
                Stream s = context.Request.InputStream;
                // Stream s = Context.Request.InputStream;
                // or  Stream s  =  HttpContext.Current.Request.InputStream;
                s.Position = 0;
                StreamReader reader = new StreamReader(s);
                string jsonText = reader.ReadToEnd();

                // Other code that converts json text to classes
            }
            catch (Exception e)
            {
                // .....
            }
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

7. "Postman". Click "SEND"; 在此处输入图片说明

8. The result, see the picture "- = RESULT = -"; 在此处输入图片说明

Server error in application '/'. Could not find this resource. Description: HTTP 404. The resource (or one of its dependencies) may have been deleted, received a different name, or may be temporarily unavailable. Review the following URL and verify that it is correct. Requested URL: / Hook Version Information: Microsoft .NET Framework Version 4.0.30319; ASP.NET version: 4.7.3062.0

Update_1
我使用链接http://localhost:44836/api/Hook
代码不止于此breakpoint
结果:
{
"Message": "Could not find the HTTP resource corresponding to the request URI \" http://localhost:44836/api/Hook\ ".", "MessageDetail": "Could not find the type corresponding to the controller \" Hook \ " . " }

我使用链接http://localhost:44836/Hook/ViberProcess
代码不止于breakpoint
结果
Server error in application '/'. For this object, no parameterless constructors are defined. Description: An unhandled exception occurred during the execution of the current web request. Examine the stack trace for more information about this error and the code snippet that caused it. Exception Details: System.MissingMethodException: No parameter-less constructors are defined for this object. Source Error: An unhandled exception occurred during the execution of the current web request. Information on the origin and location of the exception can be obtained using the following exception stack trace.

在此处输入图片说明

Cha*_*ata 8

只需删除HttpContext context您的ViberProcess操作即可。

因此,该方法将成为

public IActionResult ViberProcess()
{
    Stream s = HttpContext.Current.Request.InputStream;

    //... continue your code from here.
}
Run Code Online (Sandbox Code Playgroud)

这背后的原因是,您已经提到了HttpContext context作为的参数,ViberProcess但是在发送请求时,它将使用精确模式进行搜索。

因此,在您的请求中,您无法HttpContext从任何地方传递。因此,将永远找不到此请求。

这是屏幕截图: 码

试试这个,让我知道您是否还有问题。