Trouble accessing Message-Id in MVC4 with Mailgun delivery webhook

Hey everyone, I’m stuck trying to get the Message-Id from Mailgun’s delivered webhook in my MVC4 app. I’ve tried a few things but no luck so far.

I made a model for the webhook data:

public class MailgunDeliveryInfo
{
    public string EventType { get; set; }
    public string RecipientEmail { get; set; }
    public string DomainName { get; set; }

    [AllowHtml]
    [JsonProperty(PropertyName="DeliveryId")]
    public object DeliveryIdentifier { get; set; }

    public int DeliveryTime { get; set; }
    public string SecurityToken { get; set; }
    public string SecuritySignature { get; set; }        
}

I’ve tried setting [ValidateInput(false)] on the controller and [Bind(Exclude="delivery-details")] on the model. I even tried changing the app’s requestValidationMode to 2.0, but that feels wrong.

The DeliveryIdentifier always comes back null. Any ideas what I’m doing wrong or how to fix this? Thanks!

Hey Claire29, I’ve had similar issues with Mailgun webhooks in MVC before. Here’s what worked for me:

Instead of binding directly to a model, try reading the raw request body. You can do this by adding a custom model binder or using Request.InputStream directly in your action method.

Here’s a quick example:

[HttpPost]
[ValidateInput(false)]
public ActionResult Delivered()
{
    string rawBody;
    using (var reader = new StreamReader(Request.InputStream))
    {
        rawBody = reader.ReadToEnd();
    }
    
    var data = JsonConvert.DeserializeObject<dynamic>(rawBody);
    var messageId = data.message_headers[0][1].ToString();
    
    // Process messageId here
    
    return new HttpStatusCodeResult(200);
}

This approach bypasses the built-in model binding and gives you direct access to the webhook payload. Remember to add proper error handling and validation. Also, make sure your action is decorated with [ValidateInput(false)] to prevent request validation from interfering with the payload.

Hope this helps! Let me know if you need any clarification.

I’ve dealt with similar issues when working with Mailgun webhooks in MVC. The problem likely stems from how MVC handles complex JSON payloads. Instead of trying to bind directly to a model, I’d suggest reading the raw request body and deserializing it manually.

Try this approach in your controller:

[HttpPost]
[AllowHtml]
[ValidateInput(false)]
public ActionResult Delivered()
{
    using (var reader = new StreamReader(Request.InputStream))
    {
        var json = reader.ReadToEnd();
        var data = JsonConvert.DeserializeObject<dynamic>(json);
        var messageId = data.message_headers[0][1].ToString();
        // Process messageId here
    }
    return new HttpStatusCodeResult(200);
}

This method bypasses model binding issues and gives you direct access to the webhook payload. Remember to add appropriate error handling and validation to ensure the data is what you expect.

hey claire29, i’ve worked with mailgun webhooks before. instead of binding to a model, try reading the raw request body directly. here’s a quick example:

[HttpPost]
[ValidateInput(false)]
public ActionResult Delivered()
{
    var rawBody = new StreamReader(Request.InputStream).ReadToEnd();
    var data = JsonConvert.DeserializeObject<dynamic>(rawBody);
    var messageId = data.message_headers[0][1].ToString();
    // use messageId here
    return new HttpStatusCodeResult(200);
}

this bypasses model binding issues. let me know if u need more help!