swagger ASP.NET Core -JSON Ignore不适用于Get Request

ne5o7dgx  于 2023-08-05  发布在  .NET
关注(0)|答案(1)|浏览(105)

在ASP.NET Core Web API中,我实现了Json Ignore IN Swashbuckle Swagger,所以我做了如下所示:

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;
}

字符串
控制器:

[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);
    }
 }


我不希望AdmissionNumber和SchoolCode出现在所有(我希望他们隐藏)它的工作在HttpPost,但它不是工作在HttpGet。
我的意思是,AdmissionNumber和SchoolCode在HttpPost中的swagger上不可见(这正是我想要的)。但在HttpGet中的swagger上仍然可见。为什么AdmissionNumber和SchoolCode在HttpGet的swagger上仍然可见
我该如何解决这个问题?

vawmfj5a

vawmfj5a1#

为了不从[FromQuery]中的类获取忽略属性,需要用[BindNever]装饰属性,如下所示。

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;
    }

字符串
如需了解更多信息,请访问link

相关问题