首页 > 解决方案 > 如何使用唯一/我自己的名称注册 webhook

问题描述

摘要:如果使用这些 nuget 包,我想要一个带有特定名称的 webhook url,而不是 .../custom/ 或 .../genericjson/。

https://localhost:44300/api/webhooks/incoming/MyWebhook?code=1234567890


我发现的所有指南和类似的东西要么是针对现有的 Webhook nuget 包(Github、bitbucket、slack 等),要么是通用和自定义的(除了名称之外,我真的不明白它们的区别,但是那是另一回事)。
我启动并运行了genericjson,它正在做它应该做的事情,但我更希望有一个更正确的 url 和/或 Webhook 名称。

那么我如何得到(我认为这是我需要让它按我想要的方式工作)

config.InitializeReceiveGenericJsonWebHooks();

而是像以前一样工作

config.InitializeReceiveMyWebhookWebHooks();

因此我的服务器在上述 URL 上侦听 webhook(前提是我在 appSettings 等中添加了密钥)。

我尝试查看InitializeReceiveGenericJsonWebHooks()InitializeReceiveGitHubWebHooks()的定义,但那里几乎没有任何代码,我也不知道如何/什么/在哪里创建我自己的版本。
我对 Asp.net 很陌生(以前主要在 Apache 上用 html/css/php/javascript 编写网站,而不是在 IIS 上用 Asp.net/C# 编写网站)所以我认为应该有一些简单的方法来解决它,但是经过一整天的搜索和尝试后,我不知道接下来要尝试什么。


编辑:
目前这是我的代码(与 Webhook 相关)的样子。没什么特别的,只是让 webhook 工作的最低限度。

网页配置

  <appSettings>
    <add key="MS_WebHookReceiverSecret_GenericJson" value="1234567890123456789012345678901234567890"/>
  </appSettings>

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        //The revelant part to the question
        config.InitializeReceiveGenericJsonWebHooks();
    }
}

GenericJsonWebHookHandler.cs

namespace Project.API.Webhooks
{
    public class GenericJsonWebHookHandler : WebHookHandler
    {
        public GenericJsonWebHookHandler()
        {
            this.Receiver = "genericjson";
        }

        public override Task ExecuteAsync(string receiver, WebHookHandlerContext context)
        {
            // Get JSON from WebHook
            JObject data = context.GetDataOrDefault<JObject>();
            //Do something with webhook
            return Task.FromResult(true);
        }
    }
}

标签: c#asp.netwebhooks

解决方案


我终于设法解决了。

通过查看AspNetWebHooks 的 GitHub并进一步了解我正在使用的GenericJSON WebHook,我终于发现了缺失的内容。

唯一需要做的就是创建一个名为的文件并将HttpConfigurationExtensions.csHttpConfigurationExtensions.cs源中的代码复制+粘贴到新创建的文件中并更改

public static void InitializeReceiveGenericJsonWebHooks(this HttpConfiguration config)

我想让它听什么

public static void InitializeReceiveMyWebhookWebHooks(this HttpConfiguration config)

像这样。

然后我还需要创建一个名为并从GenericJsonWebHookReceiver.csMyWebhookReceiver.cs复制+粘贴代码并稍微更改内容的文件

    public class GenericJsonWebHookReceiver : WebHookReceiver
    {
        internal const string RecName = "genericjson";

进入

    public class MyWebhookWebHookReceiver : WebHookReceiver
    {
        internal const string RecName = "MyWebhook";

它仍然像 GenericJSON webhook(这是我想要的),但现在我可以/api/incoming/MyWebhook/whateverIWant?code=123456790用作 webhook URL 而不是让它看起来像/genericjson/MyWebhook?code=1234567890.

您仍然需要处理程序文件,但与此处的两个文件略有不同的方式相同,您可以对处理程序执行相同的操作。创建MyWebHookHandler.cs并复制+粘贴代码,更改GenericJsonMyWebHook,它应该很好。


推荐阅读