【发布时间】:2021-05-18 10:58:28
【问题描述】:
我有一个如下的 http Get 方法
public async Task<Ricetta> GetRicettaByNome(string nome)
{
Ricetta exist = default(Ricetta);
var ExistRicetta = await appDbContext.Ricetta.FirstOrDefaultAsync(n => n.Nome == nome);
if(ExistRicetta != null)
{
exist = ExistRicetta;
return exist;
}
exist = null;
return exist;
}
它被这样的控制器调用:
[HttpGet("exist/{nome}")]
public async Task<ActionResult<Ricetta>> GetRicettaByNome(string nome)
{
try
{
if (string.IsNullOrEmpty(nome))
{
return BadRequest();
}
var result = await ricetteRepository.GetRicettaByNome(nome);
if (result != null)
return result;
return default(Ricetta);
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError, "NON HAI INTERNET!");
}
}
但是当我调用我的 api 以通过 httpclient 获取响应时:
public async Task<Ricetta> GetRicettaByNome(string nome)
{
return await httpClient.GetJsonAsync<Ricetta>($"api/Ricette/exist/{nome}");
}
我收到了这个错误: 输入不包含任何 JSON 令牌。当 isFinalBlock 为真时,预期输入以有效的 JSON 令牌开头。路径:$ |行号:0 | BytePositionInLine: 0。'
【问题讨论】:
-
Ricetta是class、record还是struct?如果不是struct,则default(Ricetta)为空。
标签: httpclient blazor blazor-server-side