c# - 如何使用唯一/我自己的名称注册 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);
}
}
}
解决方案
我终于设法解决了。
通过查看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
并复制+粘贴代码,更改GenericJson
为MyWebHook
,它应该很好。
推荐阅读
- python - 使用不等数量的空格重新格式化文本文件然后转换为 csv 的更有效方法
- r - How to make a frequency table from a data frame in R
- highcharts - 多个 yAxis 时,Highcharts xAxis 有额外的间隙
- c# - AspNet.Core Identity - 无法登录用户
- android - Goodle Ad Manager SDK 与 AdMob SDK
- java - Java,如何在比较器中按字母顺序对字符进行排序
- c - 我正在尝试制作自定义 pow() 函数,但我的 for 循环不起作用 - C
- php - 使用数据库主机的最佳方式是什么?
- javascript - 输入 URL 并扫描网页以查找特定单词(请求规划建议)
- powershell - 如何将新数据插入现有文件?