【问题标题】:SendGrid Event Webhook ASP.NET MVC Post is nullSendGrid 事件 Webhook ASP.NET MVC Post 为空
【发布时间】:2017-09-19 21:56:03
【问题描述】:

我希望有人可以提供帮助。我有一个通过 Azure 运行的 ASP.NET MVC 网站。这里的最终目标是将 SendGrid 事件记录到数据库中。首先,我需要获取 SendGrid 发送的 JSON。我在Settings--> Mail Settings--> Event Notification 下打开了事件通知。在 Azure 和 Azure 远程调试中使用 Postman 和 Streaming Logs,我确定我的控制器确实接收到来自 SendGrid 的 POST 请求,但是我没有收到任何 JSON。

我尝试了以下方法:

在下面的例子中,json 为空:

public ActionResult SendGridOne(string json)

在下面的示例中,eventList 为空(注意这是使用 Sendgrid.Webhooks nuget 包):

public ActionResult SendGridOne([FromBody]SendGridEvents[] eventList)

也尝试了以下方法,但 rawSendGridJSON 始终为空:

public ActionResult SendGridOne()
        {

            System.IO.StreamReader reader = new System.IO.StreamReader(HttpContext.Request.InputStream);
            string rawSendGridJSON = reader.ReadToEnd();
            System.Diagnostics.Trace.TraceInformation(rawSendGridJSON);
            return new HttpStatusCodeResult(200);
        }

【问题讨论】:

  • 看看RequestContent-Type 是否设置为 application/json 等?
  • 我不确定 SendGrid 不是我发送请求
  • 您的控制器上有一个Request 属性。使用调试器在 SendGridOne 内部检查它。
  • 谢谢你,我没有意识到那里。 Content-Type 确实是 application/json。唯一突出的是 HttpChannelBinding 引发了“System.UnauthorizedAccessException”类型的异常。 Microsoft 文档对此没有帮助,一些 SO 帖子说它只能在调试模式下发生......
  • 尝试自己发送一些数据到您的端点,使用类似Postman

标签: c# asp.net-mvc sendgrid


【解决方案1】:

Sendgrid.Webhooks nuget 包是正确的。我用这个表格测试,它工作正常:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Sendgrid.Webhooks.Service;

namespace WebHookSendgrid.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class WebHookController : ControllerBase
    {

        // POST api/values
        [HttpPost("Sendgridwh")]
        public async Task<ActionResult> Sendgridwh([FromBody] object[] eventList)
        {
            var json = JsonConvert.SerializeObject(eventList);

            var parser = new WebhookParser();
            var events = parser.ParseEvents(json);
       
            // this is custom method, you can make yours for your needs  
            await sendGridService.EmailActivity(events);
            
            return new OkResult();
        }
    }
}

访问 Github 上的Sendgrid Webhooks

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 2013-04-09
    • 1970-01-01
    相关资源
    最近更新 更多