简体   繁体   English

SendGrid 事件 Webhook ASP.NET MVC Post 为空

[英]SendGrid Event Webhook ASP.NET MVC Post is null

I hope someone can help.我希望有人可以提供帮助。 I have an ASP.NET MVC website running via Azure.我有一个通过 Azure 运行的 ASP.NET MVC 网站。 Ultimate goal here is to log SendGrid events to a database.这里的最终目标是将 SendGrid 事件记录到数据库中。 First I need to get the JSON sent by SendGrid.首先,我需要获取 SendGrid 发送的 JSON。 I have turned on Event Notification under Settings--> Mail Settings--> Event Notification .我在Settings--> Mail Settings--> Event Notification下打开了Settings--> Mail Settings--> Event Notification Using Postman and Streaming Logs in Azure and Azure remote debugging, I have determined that my controller is indeed receiving the POST request from SendGrid, however I am not getting any JSON.在 Azure 和 Azure 远程调试中使用 Postman 和 Streaming Logs,我确定我的控制器确实正在接收来自 SendGrid 的 POST 请求,但是我没有收到任何 JSON。

I've tried the following:我尝试了以下方法:

In the example below, json is null:在下面的示例中,json 为 null:

public ActionResult SendGridOne(string json)

In the example below, eventList is null (note this is using the Sendgrid.Webhooks nuget package):在下面的示例中, eventList 为 null(注意这是使用 Sendgrid.Webhooks nuget 包):

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

Also have tried the following, but rawSendGridJSON is always null:也尝试过以下方法,但 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);
        }

The Sendgrid.Webhooks nuget package is correct. Sendgrid.Webhooks nuget 包是正确的。 I test with this form, its works correctly:我用这个表格测试,它工作正常:

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();
        }
    }
}

Visit Sendgrid Webhooks on Github访问 Github 上的Sendgrid Webhooks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM