Clear API errors [UWP]

2019-08-19 23:42发布

I have a problem with my API, the GET, POST, DELETE and PUT work perfectly. but if for some reason at the time of making a PUT or a POST I generate errors (404, 400, 500, etc) and it is normal that in some times for any query I generate errors ... the problem is that the next Once I go back to do a POST or PUT (being aware that it is ok) the error is stored and it does not let me make the new query ... I do not know what to do or what is wrong. but the GET is still working

THIS IS THE CONTROLLER CODE:

  [HttpGet("{id}")] //GET
  public async Task<IActionResult> Get(long id)
  {
     if (id == 0)
     return BadRequest();
     var tercero = await repository.GetAsync(id);
     if (tercero == null)
        return NotFound();
     else
        return Ok(tercero);
   }

   [HttpPost] //POST
   public async Task<IActionResult> Post([FromBody]Tercero tercero)
   {
     try
     {
         return Ok(await repository.InsertAsync(tercero));
     }
     catch(Exception e)
     {
         return BadRequest(e.ToString());
     }
   }

   [HttpPut("{id}")] //PUT
   public async Task<IActionResult> Put([FromBody]Tercero tercero, long Id)
   {
      if (tercero.Id != Id)
        return BadRequest("No hay correlacion entre el id y el objeto");

      try
      {
          return Ok(await repository.UpdateAsync(tercero, Id));
      }
      catch (Exception e)
      {
           return BadRequest(e.ToString());
      }
    }

    [HttpDelete("{id}")] //DELETE
    public async Task<IActionResult> Delete(long id)
    {
      try
      {
          await repository.DeleteAsync(id);
          return Ok();
      }
      catch (Exception e)
      {
          return BadRequest(e.ToString());
      }
    }

I have other controllers and with all the same thing happens. I do not know if he finds something to clean up the error

THIS IS WHAT HAPPENS

STEP 1: I do a POST with a new User

It returns the model of the query with a 200 OK

It returns the model of the query with a 200 OK

STEP 2: I generate an error giving the same code in one of the fields what the database does not allow me.

It shows me the error with a 400 Bad Request

enter image description here

STEP 3: I realize my mistake and I decide to correct it to make a correct POST

It gives me the same error

enter image description here

WHY? How can i fix it?

0条回答
登录 后发表回答