c# - 为什么我在客户端上看不到响应类的属性?
问题描述
我在我的项目中使用 .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 类的消息值,而在客户端看不到该类的其他属性?
解决方案
改变Number
和Success
从私人到公共。
默认情况下,私有成员不会序列化到响应中。
推荐阅读
- javascript - 将javascript字符串更新为数组
- python - 照片图像缩放
- ios - 在不同时间使用回调在 AVAudioPlayer 上播放 mp3 数据(base 64 编码的 mp3 字符串)?
- angular - 共享服务通过引用传递对象
- java - Java 中 JSON 的漂亮打印适用于控制台,但在浏览器中它不起作用
- python - Python 2 vs 3:从字节字符串中获取字节的结果一致
- vue.js - 为什么这里没有插入对象属性的值?
- visual-studio - (Visual Studio)为什么 C# 类/属性在自动完成中隐藏,但在键入后可见?
- angular - 添加 SVG 过滤器时,SVG 未在 IOS 上显示
- angular - 如何使用参数在 Angular 8 路由中设置约束