Asp.net core NetCore3 API未返回/序列化我的数据
我有以下控制器:Asp.net core NetCore3 API未返回/序列化我的数据,asp.net-core,.net-core,asp.net-core-3.0,ef-core-3.1,Asp.net Core,.net Core,Asp.net Core 3.0,Ef Core 3.1,我有以下控制器: [HttpGet("idfull/{id}")] public async Task<IActionResult> GetAccountByIdFull(int id) { try { var response = await _accountFacade.GetAccountByIdAsync(id, full: true).ConfigureAwait
[HttpGet("idfull/{id}")]
public async Task<IActionResult> GetAccountByIdFull(int id)
{
try
{
var response = await _accountFacade.GetAccountByIdAsync(id, full: true).ConfigureAwait(false);
if (response == null)
return NoContent();
return Ok(response);
}
catch (KeyNotFoundException kEx)
{
return NotFound();
}
catch (Exception ex)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
}
为什么API没有返回JSON对象?您可以在没有
的情况下尝试。ConfigureAwait(false)
除此之外,您的代码似乎还可以,API应该可以正常工作
如果您没有,我建议您在API中添加Swagger,这只是5-10行代码的问题,在开发阶段会有很大帮助(并且在测试时避免手动错误)。以下是官方文件:
您还可以尝试在控制器中添加一个非常愚蠢的操作,以尝试确定问题的来源(控制器层?服务层?门面层?)您是否可以在没有
的情况下尝试。ConfigureWait(false)
除此之外,您的代码似乎还可以,API应该可以正常工作
如果您没有,我建议您在API中添加Swagger,这只是5-10行代码的问题,在开发阶段会有很大帮助(并且在测试时避免手动错误)。以下是官方文件:
您还可以尝试在控制器中添加一个非常愚蠢的操作,以尝试确定问题的来源(控制器层?服务层?门面层?)您说API挂起。所以前端并没有收到200个ok响应,对吗?这意味着您无法从服务器获得响应。看不到任何序列化问题。尝试通过RESTAPI客户端(如Postman)调用服务器端API并显示结果。Swagger模仿Postman,甚至Swagger也没有得到响应。当我调试时,我可以看到OK被返回,或者至少,调试器在它上面的步骤。。。你是否收到回复?您只收到空的200 ok或API挂起,您无法得到任何响应?我没有收到任何响应如果这样,您需要检查您的API是否可访问。您没有序列化问题。存在可访问/阻塞/部署或smth问题。试着用不同的工具检查你的API,你说API挂起了。所以前端并没有收到200个ok响应,对吗?这意味着您无法从服务器获得响应。看不到任何序列化问题。尝试通过RESTAPI客户端(如Postman)调用服务器端API并显示结果。Swagger模仿Postman,甚至Swagger也没有得到响应。当我调试时,我可以看到OK被返回,或者至少,调试器在它上面的步骤。。。你是否收到回复?您只收到空的200 ok或API挂起,您无法得到任何响应?我没有收到任何响应如果这样,您需要检查您的API是否可访问。您没有序列化问题。存在可访问/阻塞/部署或smth问题。试着用不同的工具检查你的API我正在使用斯威格,在我最初的问题中说:-)哦,对不起。。。那么,您可以尝试不配置等待吗?我从来没有用过这个方法。你能发布你的启动文件吗?Add()和Use()的顺序很重要,有些问题就是由此而来的。我使用的是招摇过市,在我最初的问题中声明:-)哦,对不起。。。那么,您可以尝试不配置等待吗?我从来没有用过这个方法。你能发布你的启动文件吗?Add()和Use()的顺序很重要,因此会产生一些问题。
public async Task<AccountViewModel> GetAccountByIdAsync(int accountId, bool full = false)
{
try
{
var unmappedResponse = await _accountService.GetAccountByIdAsync(accountId, full);
var mappedResponse = _mapper.Map<AccountViewModel>(unmappedResponse);
return mappedResponse;
}
catch
{
throw;
}
}
public async Task<Account> GetAccountByIdAsync(int accountId, bool full = false)
{
try
{
Account account;
if (full)
{
account = await _repo.GetOneAsync<Account>(x => x.AccountId == accountId);
account.Company = await _repo.GetOneAsync<Company>(filter: x => x.CompanyId == account.CompanyId,
includes: source => source
.Include(c => c.CompanyTransferType)
.ThenInclude(ctt => ctt.PartnerCompanyAccountType)
.Include(c => c.CompanyTransferType).ThenInclude(ctt => ctt.TransferType)
.Include(c => c.CompanyEquipment).ThenInclude(ce => ce.Equipment)
.Include(c => c.CompanyAccountGroup)
.Include(c => c.CompanyAccountType));
account.AccountContact = await _repo.GetAsync<AccountContact>(filter: x => x.AccountId == accountId);
account.AccountEquipment = await _repo.GetAsync<AccountEquipment>(filter: x => x.AccountId == accountId,
includes: source => source
.Include(ae => ae.AccountEquipmentFee).Include(ae => ae.CompanyEquipment).ThenInclude(ce => ce.Equipment));
account.AccountPickVolumeDefaultAccount = await _repo.GetAsync<AccountPickVolumeDefault>(filter: x => x.AccountId == accountId,
includes: source => source
.Include(a => a.Equipment).Include(a => a.PartnerAccount));
}
else
{
account = await _repo.GetByIdAsync<Account>(accountId);
}
if (account == null)
throw new KeyNotFoundException($"Could not find Account with ID: {accountId}");
return account;
}
catch
{
throw;
}
}
services.AddControllers().AddNewtonsoftJson(setup =>
{
setup.SerializerSettings.ContractResolver = new DefaultContractResolver();
setup.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});