Ayobamilaye
Ayobamilaye

Reputation: 1563

ASP.NET Core -JSON Ignore not working for Get Request

In ASP.NET Core Web API, I implemented Json Ignore IN Swashbuckle Swagger So I did as shown below:

public class QueryChequeBookRequestDto : GetCreateChequeDetailDto
{
    [Required(ErrorMessage = "Account Number field is required.")]
    [JsonProperty("AccountNumber")]
    public string AccountNumber { get; set; }
}

public class GetCreateChequeDetailDto
{
    [System.Text.Json.Serialization.JsonIgnore]
    [Newtonsoft.Json.JsonProperty("AdmissionNumber")]
    public string AdmissionNumber { get; set; } = null;

    [System.Text.Json.Serialization.JsonIgnore]
    [Newtonsoft.Json.JsonProperty("SchoolCode")]
    public string SchoolCode { get; set; } = null;
}

Controller:

[Produces("application/json")]
[Consumes("application/json")]
[Route("api/[controller]")]
[ApiController]
public class ChequeBookController : ControllerBase
{
    private readonly IChequeBooksServices _chequeBooksServices;
    public ChequeBookController(
        IChequeBooksServices chequeBooksServices
        )
    {
        _chequeBooksServices = chequeBooksServices;
    }

    [HttpGet("QueryChequeBook")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public async Task<IActionResult> GetQueryChqueBookAsync([FromQuery] QueryChequeBookRequestDto chequeBookDto)
    {
        var result = await _chequeBooksServices.GetQueryChequeBookAsync(chequeBookDto);
        return StatusCode(result.StatusCode, result);
    }
 }

I don't want AdmissionNumber and SchoolCode to appear at all (I want them hidden) It's working on HttpPost, but it's not working on HttpGet.

What I mean is that AdmissionNumber and SchoolCode are not visible on swagger in HttpPost (which is what I want). But are still visible on swagger in HttpGet. Why are AdmissionNumber and SchoolCode still visible on swagger in HttpGet

How do I resolve this?

Upvotes: 0

Views: 552

Answers (1)

Upender Reddy
Upender Reddy

Reputation: 618

Not to get ignore Properties from a class in [FromQuery] need to decorate Property with [BindNever] as follows.

   public class GetCreateChequeDetailDto
    {
        [BindNever]
        [System.Text.Json.Serialization.JsonIgnore]
        [Newtonsoft.Json.JsonProperty("AdmissionNumber")]
        public string AdmissionNumber { get; set; } = null;

        [BindNever]
        [System.Text.Json.Serialization.JsonIgnore]
        [Newtonsoft.Json.JsonProperty("SchoolCode")]
        public string SchoolCode { get; set; } = null;
    }

for more follow this link

Upvotes: 1

Related Questions