摘要:我希望有一个带有特定名称的 webhook 网址,而不是 .../custom/或 .../genericjson/如果使用那些 nuget 包。
即 https://localhost:44300/api/webhooks/incoming/MyWebhook?code=1234567890
我找到的所有指南和类似的东西要么是针对现有的 Webhook nuget 包(Github、bitbucket、slack 等)的,要么是通用和自定义的(除了名称之外,我真的不明白其中的区别,但那是另一回事)。
我启动并运行了genericjson并且它正在做它应该做的事情,但我宁愿为 Webhook 提供一个更正确的 url 和/或名称。
那么我如何获得(我认为这是我需要的让它按我想要的方式工作)
config.InitializeReceiveGenericJsonWebHooks();
而是像以前一样工作
config.InitializeReceiveMyWebhookWebHooks();
因此,我的服务器侦听上述 URL 上的网络钩子(前提是我在 appSettings 中添加密钥等)。
我尝试查看InitializeReceiveGenericJsonWebHooks() 和 InitializeReceiveGitHubWebHooks()的定义,但那里几乎没有任何代码,我也不知道如何/什么/在哪里创建我自己的版本。
我对 Asp.net 很陌生(以前主要在 Apache 上用 html/css/php/javascript 编码网站,而不是 IIS 上的 Asp.net/C#),所以我侦察应该有一些简单的方法来解决它,但经过一整天的搜索和尝试,我结束了下一步该尝试什么。
编辑:
目前这是我的代码(与Webhook相关)的样子。没什么特别的,只是最低限度地让网络钩子工作。
网络.config
<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);
}
}
}
我终于设法解决了它。
通过查看AspNetWebHooks的GitHub,以及我正在使用的GenericJSON WebHook,我终于发现了缺少的内容。
唯一需要的是创建一个名为HttpConfigurationExtensions.cs
的文件,并将 HttpConfigurationExtensions.cs 源代码中的代码复制+粘贴到新创建的文件中并更改
public static void InitializeReceiveGenericJsonWebHooks(this HttpConfiguration config)
我想让它听什么
public static void InitializeReceiveMyWebhookWebHooks(this HttpConfiguration config)
这样。
然后我还需要创建一个名为MyWebhookReceiver.cs
的文件,并从GenericJsonWebHookReceiver复制+粘贴代码.cs并稍微更改内容
public class GenericJsonWebHookReceiver : WebHookReceiver
{
internal const string RecName = "genericjson";
到
public class MyWebhookWebHookReceiver : WebHookReceiver
{
internal const string RecName = "MyWebhook";
它仍然像泛型JSON网络钩子(这是我想要的),但现在我可以/api/incoming/MyWebhook/whateverIWant?code=123456790
用作网络钩子URL,而不是让它看起来像/genericjson/MyWebhook?code=1234567890
。
您仍然需要处理程序文件,但以相同的方式,这两个文件在这里略有更改,您可以对处理程序执行相同的操作。创建MyWebHookHandler.cs
,只需复制+粘贴代码,将GenericJson
更改为MyWebHook
,应该很好。
干得好!如果其他人正在关注这一点,那么唯一缺少的几件事是 Web.config 条目
<add key="MS_WebHookReceiverSecret_GenericJson" value="i=xxx,z=xxx" />
成为:
<add key="MS_WebHookReceiverSecret_MyWebhook" value="i=xxx,z=xxx" />
由于键名称的最后一部分需要与最后一部分中新命名约定的名称匹配。
和处理程序本身 - 在构造函数上:
应该是:
public MyWebhookHandler()
{
this.Receiver = GenericJsonWebHookReceiver.ReceiverName;
}
不:
public MyWebhookHandler()
{
this.Receiver = "genericjson";
}
另外 - 执行 HttpConfigurationExtensions.cs 步骤没有任何好处。
但是,您需要执行 GenericJsonWebHookReceiver.cs 步骤才能使该过程正常工作。