ASP.Net Web API 存储库更新错误 500
ASP.Net Web API Repository Update Error 500
我的网站 Api 控制器有一个 UpdateArea
方法,该方法通过 id
和正文中的更新值。写法如下:
[HttpPut("{id}")]
public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateDto)
{
// Check that the 'area' object parameter can be de-serialised to a AreaForUpdateDto.
if (areaForUpdateDto == null)
{
_logger.LogError("AreasController.UpdateArea failed: Could not map area object parameter to a AreaForUpdateDto");
// Return an error 400.
return BadRequest();
}
// Ensure that the Area exists.
var areaEntityFromRepo = _areaRepository.GetArea(id);
// Map(source object (Dto), destination object (Entity))
Mapper.Map(areaForUpdateDto, areaEntityFromRepo);
_areaRepository.UpdateArea(areaEntityFromRepo);
// Save the updated Area entity, added to the DbContext, to the SQL database.
if (await _areaRepository.SaveChangesAsync())
{
var areaFromRepo = _areaRepository.GetArea(id);
if (areaFromRepo == null)
return NotFound();
var area = Mapper.Map<AreaDto>(areaFromRepo);
return Ok(area);
}
// The save has failed.
_logger.LogWarning($"Updating Area {id} failed on save.");
throw new Exception($"Updating Area {id} failed on save.");
}
在我的应用程序要求更新之前,这工作正常,但请求的主体与数据库中的现有数据相同。发生这种情况时,_areaRepository.SaveChangesAsync()
无法返回 error 500
。
如果用户打开“编辑详细信息”对话框,不做任何更改,然后单击发送请求的“编辑”按钮,就会发生这种情况。
我可以在客户端上验证,这很好,但我很惊讶这会导致错误。
任何人都可以解释为什么会失败以及我应该在哪里更正它吗?
按照你解释的逻辑
This happens if the user opens an Edit Details dialog, does not change anything and then clicks the Edit button sending the request.
和这段代码
//...code removed for brevity
// Save the updated Area entity, added to the DbContext, to the SQL database.
if (await _areaRepository.SaveChangesAsync())
{
var areaFromRepo = _areaRepository.GetArea(id);
if (areaFromRepo == null)
return NotFound();
var area = Mapper.Map<AreaDto>(areaFromRepo);
return Ok(area);
}
// The save has failed.
_logger.LogWarning($"Updating Area {id} failed on save.");
throw new Exception($"Updating Area {id} failed on save.");
如果未对文件进行任何更改并尝试进行更新,则不会对 DbContext
进行任何更改,这意味着 _areaRepository.SaveChangesAsync()
将是 false
并因此继续至
// The save has failed.
_logger.LogWarning($"Updating Area {id} failed on save.");
throw new Exception($"Updating Area {id} failed on save.");
和BAM 服务器上出现 500 错误。
我建议debugging/stepping通过可疑代码确认其行为如上所述,然后重新思考和重构更新逻辑。
我的网站 Api 控制器有一个 UpdateArea
方法,该方法通过 id
和正文中的更新值。写法如下:
[HttpPut("{id}")]
public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateDto)
{
// Check that the 'area' object parameter can be de-serialised to a AreaForUpdateDto.
if (areaForUpdateDto == null)
{
_logger.LogError("AreasController.UpdateArea failed: Could not map area object parameter to a AreaForUpdateDto");
// Return an error 400.
return BadRequest();
}
// Ensure that the Area exists.
var areaEntityFromRepo = _areaRepository.GetArea(id);
// Map(source object (Dto), destination object (Entity))
Mapper.Map(areaForUpdateDto, areaEntityFromRepo);
_areaRepository.UpdateArea(areaEntityFromRepo);
// Save the updated Area entity, added to the DbContext, to the SQL database.
if (await _areaRepository.SaveChangesAsync())
{
var areaFromRepo = _areaRepository.GetArea(id);
if (areaFromRepo == null)
return NotFound();
var area = Mapper.Map<AreaDto>(areaFromRepo);
return Ok(area);
}
// The save has failed.
_logger.LogWarning($"Updating Area {id} failed on save.");
throw new Exception($"Updating Area {id} failed on save.");
}
在我的应用程序要求更新之前,这工作正常,但请求的主体与数据库中的现有数据相同。发生这种情况时,_areaRepository.SaveChangesAsync()
无法返回 error 500
。
如果用户打开“编辑详细信息”对话框,不做任何更改,然后单击发送请求的“编辑”按钮,就会发生这种情况。
我可以在客户端上验证,这很好,但我很惊讶这会导致错误。
任何人都可以解释为什么会失败以及我应该在哪里更正它吗?
按照你解释的逻辑
This happens if the user opens an Edit Details dialog, does not change anything and then clicks the Edit button sending the request.
和这段代码
//...code removed for brevity
// Save the updated Area entity, added to the DbContext, to the SQL database.
if (await _areaRepository.SaveChangesAsync())
{
var areaFromRepo = _areaRepository.GetArea(id);
if (areaFromRepo == null)
return NotFound();
var area = Mapper.Map<AreaDto>(areaFromRepo);
return Ok(area);
}
// The save has failed.
_logger.LogWarning($"Updating Area {id} failed on save.");
throw new Exception($"Updating Area {id} failed on save.");
如果未对文件进行任何更改并尝试进行更新,则不会对 DbContext
进行任何更改,这意味着 _areaRepository.SaveChangesAsync()
将是 false
并因此继续至
// The save has failed.
_logger.LogWarning($"Updating Area {id} failed on save.");
throw new Exception($"Updating Area {id} failed on save.");
和BAM 服务器上出现 500 错误。
我建议debugging/stepping通过可疑代码确认其行为如上所述,然后重新思考和重构更新逻辑。