Anchor for Header

Sendgrid webhook for .Net core working example

Brett Andrew 8th April 2022

Anchor for Text

So this was annoying, over 16 hours to get to the point where I could save the data, so hope to save some of you the same. Subscribe as payment if you feel it was worthy, see link at the bottom.

Anchor for Text


After reading the manual

https://docs.sendgrid.com/for-developers/tracking-events/getting-started-event-webhook

I still had questions and I couldn't find a really simple (1 function) solution to this so I used a combination of many solutions and came up with this "mini". version.

Once you deploy this to your site, go into send grid Settings > Mail  Settings and add the url to your post and test it out.


Anchor for DisplayCode
//this is the class model we use to deserialize the post from sendgrid, its not complete but works well with this minimal data

public class SendGridSentEvent
    {
        public string @event { get; set; }
        public string email { get; set; }
        public object category { get; set; }
        public string response { get; set; }
        public string attempt { get; set; }
        public string timestamp { get; set; }
        public string url { get; set; }
        public string status { get; set; }
        public string reason { get; set; }
        public string type { get; set; }
    }

[Produces("application/json")]
        [HttpPost("[action]")]        
        public void PostDataToMe()
        {
            IData Data = (IData)ControllerContext.HttpContext.RequestServices.GetService(typeof(IData));
            IHelper Helper = (IHelper)ControllerContext.HttpContext.RequestServices.GetService(typeof(IHelper));

            string eventListstring = "";
            using (StreamReader reader = new StreamReader(Request.Body, System.Text.Encoding.UTF8))
            {
                eventListstring =  reader.ReadToEndAsync().Result;
            }


            try
            {
               IEnumerable<SendGridSentEvent> eventList = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<SendGridSentEvent>>(eventListstring);

//The built in .net core json converter does not work FYI
                //SendGridSentEventList eventList = System.Text.Json.JsonSerializer.Deserialize<SendGridSentEventList>(eventListstring);




                if (eventList.Count() > 0)
                {
                    foreach (SendGridSentEvent thisevent in eventList)
                    {
                        //do something with your event here
                        //var webhookEvent = thisevent;
                        //Data.Save<Members_CommunicationWebhook>(webhookEvent);
                       //also watch out as many of the fields sometimes have nulls, so check before converting to strings.
                    }
                }
            }
            catch(Exception e)
            {

      //Helper.DebugLog("Sendgrid", "SendGridEventPost data : " + eventListstring, e)

            }

           
        }