具有 Null 值的 ReadFromJsonAsync returns 个对象属性
ReadFromJsonAsync returns object properties with Null values
我有一个 Blazor WebAssembly 应用程序(ASP.Net 核心托管的渐进式 Web 应用程序)具有以下逻辑:
客户:
protected override async Task OnInitializedAsync()
{
string token = await _loginService.GetToken();
_http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var result = await _http.PostAsJsonAsync("api/api/getcalllogs", userCallsRequestFilters);
if (result.IsSuccessStatusCode)
{
var morUserCallLogs = await result.Content.ReadFromJsonAsync<MorUserCallLogsResponse>();
await js.InvokeAsync<object>("TestDataTablesAdd", "#example");
}
else
{
morUserCallLogs = new MorUserCallLogsResponse();
}
}
服务器:(服务器端 API 我有以下代码可以按预期工作:)
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class MorApiController : ControllerBase
...
[HttpPost("getcalllogs")]
public async Task<MorUserCallLogsResponse> GetCallLogs ([FromBody] MorUserCallsRequestFilters filters)
{
...
return result;
服务器端控制器 API 填充模型正常,当我检查时,我看到以下快照(*为了安全起见,一些值已被删除)
模型:(我的 MorUserCallLogsResponse 模型如下所示:)
namespace MyNumberV2.Model
{
public class MorUserCallLogsResponse
{
public MorUserCallLogsResponse()
{
Calls = new List<MorCall>();
}
public string Error { get; set; }
public bool IsSuccessfull { get; set; }
public string userid;
public string username;
...
...
public List<MorCall> Calls { get; set; }
public class MorCall
{
public string calldate2;
public string timezone;
...
...
}
}
返回 blazor,当我尝试在以下行中读取这个返回的对象时:
var morUserCallLogs = await result.Content.ReadFromJsonAsync<MorUserCallLogsResponse>();
我检索到的模型如下所示:
如您所见,我检索到的模型包含具有 140 个嵌套调用对象模型的所有属性,但是所有属性都是 NULL...
忘记加get了;放;对于我所有的模型属性...
{ 得到;放; }
public string Error { get; set; }
public bool IsSuccessfull { get; set; }
public string userid { get; set; }
public string username { get; set; }
.....
"api/api/getcalllogs"
不匹配 [Route("api/[controller]")]
和 [HttpPost("getcalllogs")]
根据我的阅读,Url 应该是 "api/MorApi/getcalllogs"
但话又说回来,这应该会产生 404 或 400
我不得不使用 属性 注释中的以下内容
using System.Text.Json.Serialization
[JsonPropertyName("access_token")]
public string AccessToken { get; set; }
我有一个 Blazor WebAssembly 应用程序(ASP.Net 核心托管的渐进式 Web 应用程序)具有以下逻辑:
客户:
protected override async Task OnInitializedAsync()
{
string token = await _loginService.GetToken();
_http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var result = await _http.PostAsJsonAsync("api/api/getcalllogs", userCallsRequestFilters);
if (result.IsSuccessStatusCode)
{
var morUserCallLogs = await result.Content.ReadFromJsonAsync<MorUserCallLogsResponse>();
await js.InvokeAsync<object>("TestDataTablesAdd", "#example");
}
else
{
morUserCallLogs = new MorUserCallLogsResponse();
}
}
服务器:(服务器端 API 我有以下代码可以按预期工作:)
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class MorApiController : ControllerBase
...
[HttpPost("getcalllogs")]
public async Task<MorUserCallLogsResponse> GetCallLogs ([FromBody] MorUserCallsRequestFilters filters)
{
...
return result;
服务器端控制器 API 填充模型正常,当我检查时,我看到以下快照(*为了安全起见,一些值已被删除)
模型:(我的 MorUserCallLogsResponse 模型如下所示:)
namespace MyNumberV2.Model
{
public class MorUserCallLogsResponse
{
public MorUserCallLogsResponse()
{
Calls = new List<MorCall>();
}
public string Error { get; set; }
public bool IsSuccessfull { get; set; }
public string userid;
public string username;
...
...
public List<MorCall> Calls { get; set; }
public class MorCall
{
public string calldate2;
public string timezone;
...
...
}
}
返回 blazor,当我尝试在以下行中读取这个返回的对象时:
var morUserCallLogs = await result.Content.ReadFromJsonAsync<MorUserCallLogsResponse>();
我检索到的模型如下所示:
如您所见,我检索到的模型包含具有 140 个嵌套调用对象模型的所有属性,但是所有属性都是 NULL...
忘记加get了;放;对于我所有的模型属性...
{ 得到;放; }
public string Error { get; set; }
public bool IsSuccessfull { get; set; }
public string userid { get; set; }
public string username { get; set; }
.....
"api/api/getcalllogs"
不匹配 [Route("api/[controller]")]
和 [HttpPost("getcalllogs")]
根据我的阅读,Url 应该是 "api/MorApi/getcalllogs"
但话又说回来,这应该会产生 404 或 400
我不得不使用 属性 注释中的以下内容
using System.Text.Json.Serialization
[JsonPropertyName("access_token")]
public string AccessToken { get; set; }