C# ASP.Net Web API存储库更新错误500
我的Web Api控制器有一个C# ASP.Net Web API存储库更新错误500,c#,asp.net-core-webapi,C#,Asp.net Core Webapi,我的Web 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
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.");
}
[HttpPut(“{id}”)]
公共异步任务UpdateArea(Guid id,[FromBody]AreaForUpdateDto AreaForUpdateDto)
{
//检查“area”对象参数是否可以反序列化为AreaForUpdateTo。
if(areaForUpdateDto==null)
{
_logger.LogError(“AreasController.UpdateArea失败:无法将area对象参数映射到AreaForUpdateDto”);
//返回一个错误400。
返回请求();
}
//确保该区域存在。
var areaEntityFromRepo=\u areaRepository.GetArea(id);
//映射(源对象(Dto)、目标对象(实体))
Mapper.Map(areaForUpdateDto,areaEntityFromRepo);
_areaRepository.UpdateArea(areaEntityFromRepo);
//将添加到DbContext的更新区域实体保存到SQL数据库。
if(wait_areaRepository.saveChangesSync())
{
var areaFromRepo=\u areaRepository.GetArea(id);
如果(areaFromRepo==null)
返回NotFound();
var area=Mapper.Map(areaFromRepo);
返回Ok(区域);
}
//保存失败。
_logger.LogWarning($“保存时更新区域{id}失败”);
抛出新异常($“保存时更新区域{id}失败。”);
}
在我的应用程序调用更新之前,这可以正常工作,但请求的主体与数据库中的现有数据相同。发生这种情况时,\u areaRepository.saveChangesSync()
无法返回错误500
如果用户打开“编辑详细信息”对话框,不更改任何内容,然后单击“编辑”按钮发送请求,则会发生这种情况
我可以在客户机上进行验证,这很好,但我很惊讶这会导致错误
有人能解释一下为什么失败,我应该在哪里改正吗?根据你解释的逻辑 如果用户打开“编辑详细信息”对话框,不更改任何内容,然后单击“编辑”按钮发送请求,则会发生这种情况 这段代码
//...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.");
和BAM500服务器错误
我建议对可疑代码进行调试/单步执行,以确认其行为如上文所述,然后重新考虑并重构更新逻辑。HTTP 500表示服务器端异常。例外情况是什么?当然,
savechangesync
是否返回bool
或int
是。因此,在这种情况下,我应该在控制器中以某种方式更优雅地处理这个问题,还是应该阻止请求被发出?@TDC这完全取决于您。如果没有要发送到服务器的更改,则保存行程。您也不能抛出异常并向客户端返回模型状态消息。我想我将在发送相等请求之前对对象进行评估。谢谢你迅速而简洁的回答。
// The save has failed.
_logger.LogWarning($"Updating Area {id} failed on save.");
throw new Exception($"Updating Area {id} failed on save.");