ASP.Net Web API repository update error 500


Trade Development Council

My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows:

[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.");
}

This works fine until my app asks for an update, but the body of the request is the same as the existing data in the database. When this happens, _areaRepository.SaveChangesAsync()failure is returned error 500.

This happens if the user opens the Edit Details dialog, makes no changes, and then clicks the Edit button that sends the request.

I can validate on the client side, which is fine, but to my surprise this results in an error.

Can anyone explain why this fails and where should I correct it?

Nkosi

According to the logic you explain

This happens if the user opens the Edit Details dialog, makes no changes, and then clicks the Edit button that sends the request.

and this code

//...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.");

If no changes have been made to the file and an update is attempted, no changes will be made, DbContextwhich means the process _areaRepository.SaveChangesAsync()will falsecontinue until

// The save has failed.
_logger.LogWarning($"Updating Area {id} failed on save.");
throw new Exception($"Updating Area {id} failed on save.");

and a BAM 500 error on the server .

I recommend debugging/stepping through the suspect code to confirm that it behaves as described above, then reconsider and refactor the update logic.

Related


ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.Net Web API repository update error 500

Trade Development Council My Web Api controller has a method UpdateAreathat is passed and updated value idin the body . The content is as follows: [HttpPut("{id}")] public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateD

ASP.NET Core Web API 500 Internal Server Error

Fear I am trying to build an API within the ASP.NET Core framework. I made a basic controller that just returns a string: namespace Dojo_Api.Controllers.Forum { //[Authorize] [Route("api/[controller")] public class ForumController : Controller

Asp.Net Core 2 Web API 500 Internal Server Error

Anil When I try to access the API through postman, send: localhost:5050/api/Auth/token body: {“ UserName”:“ jouverc”,“ Password”:“ P @ ssw0rd!” } This method: [Produces("application/json")] [Route("api/Auth")] public class AuthController : Controller { #r

Asp.Net Core 2 Web API 500 Internal Server Error

Anil When I try to access the API through postman, send: localhost:5050/api/Auth/token body: {“ UserName”:“ jouverc”,“ Password”:“ P @ ssw0rd!” } This method: [Produces("application/json")] [Route("api/Auth")] public class AuthController : Controller { #r

Asp.Net Core 2 Web API 500 Internal Server Error

Anil When I try to access the API through postman, send: localhost:5050/api/Auth/token body: {“ UserName”:“ jouverc”,“ Password”:“ P @ ssw0rd!” } This method: [Produces("application/json")] [Route("api/Auth")] public class AuthController : Controller { #r

Asp.Net Core 2 Web API 500 Internal Server Error

Anil When I try to access the API through postman, send: localhost:5050/api/Auth/token body: {“ UserName”:“ jouverc”,“ Password”:“ P @ ssw0rd!” } This method: [Produces("application/json")] [Route("api/Auth")] public class AuthController : Controller { #r

Asp.Net Core 2 Web API 500 Internal Server Error

Anil When I try to access the API through postman, send: localhost:5050/api/Auth/token body: {“ UserName”:“ jouverc”,“ Password”:“ P @ ssw0rd!” } This method: [Produces("application/json")] [Route("api/Auth")] public class AuthController : Controller { #r

500 error when deploying asp.net core web api to IIS 7.5

username I have an asp.net core web API project that I am trying to host in IIS 7.5. As suggested in the following article, Could not find configuration file 'appsettings.json', it is not optional Below is my "appsettings.json" file, { "ConnectionStrings": {

Asp.Net Core 2 Web API 500 Internal Server Error

Anil When I try to access the API through postman, send: localhost:5050/api/Auth/token Body: {“ UserName”:“ jouverc”,“ Password”:“ P @ ssw0rd!” } This method: [Produces("application/json")] [Route("api/Auth")] public class AuthController : Controller { #r