0

I'm pretty new to C# and ASP.NET. I have an API controller with this action method:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Mautic.Webhook 
{
    [ApiController]
    [Route("mautic/[controller]")]
    public class WebhookController : ControllerBase
    {
        public WebhookController(ILogger<WebhookController> logger)
        {
            Logger = logger;
        }

        [HttpPost]
        public ActionResult WebhookAction(WebhookRequest data)
        {
            Logger.LogInformation(ObjectDumper.Dump(data.LeadPostDelete));
            return NoContent();
        }

        private ILogger<WebhookController> Logger { get; }
    }
}

And have this definition of WebhookRequest:

using System.Collections.Generic;
using Newtonsoft.Json;

namespace Mautic 
{
    public class ContactWebhookRequest 
    {
        public int id { get; set; }
        public string timestamp { get; set; }
    }

    public class WebhookRequest
    {
       [JsonProperty(PropertyName = "mautic.lead_post_delete")]
       public List<ContactWebhookRequest> LeadPostDelete { get; set; }
       public string timestamp { get; set; }
    }
}

When I receive data in the controller I want it to deserialize to my class but I got null on LeadPostDelete property. Does ASP.NET understand the Newtonsoft JsonProperty attribute? Or do I need to migrate it to System.Text.Json?

Also migrating to System.Text.Json is enough or I will need to deserialize this by hand on my controller?

7
  • What is the JSON request that you are sending and what is the content-type? Commented May 5, 2020 at 20:14
  • 1
    .Net core from version 3 "understands" both (but only one can be configured at once I think). You can figure out which one did you configure to use. I think System.Text.Json is default, so Newtonsoft attribute won't work out of the box Commented May 5, 2020 at 20:16
  • application/json and an example of request data can be found here: gist.github.com/dhilst/046227d3d0e9d05b055c92fcbec72ac7 Commented May 5, 2020 at 20:17
  • Ilya, I want to keep with "out of the box" solution, so migrating to System.text.Json should help me right? Do I need to install System.Text.Json? I saw that in the MS docs, it doesn't really make much sense to me Commented May 5, 2020 at 20:19
  • @IlyaChernomordik thanks, I migrated to System.Text.Json, now I'm getting a validation error, there should be a mismatch between my class and the received JSON, I will do further investigation thank you again! Commented May 5, 2020 at 20:36

2 Answers 2

1

JsonProperty attribute does exist in both "old" Newtonsoft.Json that was default in .Net Core before version 3. You are probably using the new System.Text.Json that won't understand this attribute out of the box.

You need probably to use the attribute from that namespace. Otherwise ensure that you use the same serialization and attribute anyway.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want avoid refactoring on migrating to 3+ you should to force controllers to use Newtonsoft.Json:

https://dotnetcoretutorials.com/2019/12/19/using-newtonsoft-json-in-net-core-3-projects/

Sorry for My English.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.