首页 > 解决方案 > 为什么我在客户端上看不到响应类的属性?

问题描述

我在我的项目中使用 .net core 5。

我有这个api方法:

    [HttpGet]
    public async Task<NumberValidationResponse> CheckNumberAsync(String number)
    {
        try
        {
            var result = await _service.someMethodn(number);
            return result;
        }
        catch (Exception ex) 
        {
            _logger.LogInformation(ex.InnerException.ToString());  //write to file or database
            return new NumberValidationResponse(null, false, "An error occurred, contact administrator.");
        }
    }
    

如您所见,API 方法的返回类型是 Task。

这是 NumberValidationResponse 定义:

    public class NumberValidationResponse : BaseResponse
    {
        private string Number { get; set; }
        private bool Success { get; set; }

        public NumberValidationResponse(string number, bool isPositive, string message) : base(message) 
        {
            Number = number;
            Success = isPositive;
        }
    }

    public abstract class BaseResponse
    {
        public string Message { get; set; }

        public BaseResponse(string message)
        {
            Message = message;
        }
    }
    

CheckNumberAsync_service.someMethod中的返回 NumberValidationResponse 类型,其值为 number、success 和 message。我希望在客户端上看到相同的值,但在客户端中,我只看到 message 属性的值。

为什么我只看到 NumberValidationResponse 类的消息值,而在客户端看不到该类的其他属性?

标签: c#asp.net-coreasp.net-web-api

解决方案


改变NumberSuccess私人公共

默认情况下,私有成员不会序列化到响应中。


推荐阅读