Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Glenatron 6 posts 76 karma points
    Mar 23, 2023 @ 15:02
    Glenatron
    0

    How to use [JsonIgnore] attributes on a service in a u10 site

    I have of API endpoints in my project. They typically use a model like this:

    using Newtonsoft.Json;
    
    public class MyModel
    {
       [JsonIgnore]
       public int NodeId { get; set; }
    
       [JsonProperty(PropertyName = "title")]
       public string Name { get; set; }
    
       public string Reference { get; set; }
    }
    

    And a controller like this:

        [ApiController]
        [Route("api/my-endpoint")]
        public sealed class MyEndpointApiController : Controller
        {
             [HttpGet]
             [Route("all")] 
             [ProducesResponseType(typeof(List<MyModel>), StatusCodes.Status200OK)]
            public ActionResult GetAllHTQualifications()
            {
                List<MyModel> models = GetAll<MyModel>();
                return Ok(models);
            }
        }
    

    This request returns a response, but the NodeId is still there in the data and Name has not been changed to title. Is there something I need to do to force it to use the NewtonSoft attributes or should I be changing over to the .Net Core serialisation attributes instead? I have tried using those and they don't seem to work either. What do I need to do to get my Json objects serialised in a way that respects their attributes?

Please Sign in or register to post replies

Write your reply to:

Draft