Service is not being called to update the database

Asked

Viewed 502 times

1

I did a job and when I call on URL, passing the appropriate parameters, does not work:

Call on URL:

http://localhost:9078/api/atualiza/1000012120/teste

My service:

public class AtualizaController : ApiController
    {
        AutorizadorContext contexto = new AutorizadorContext();
        PedidoLiberacao liberacao = new PedidoLiberacao();

        [AcceptVerbs("Put")]
        public void putItensLiberacao(int id, [FromBody]string value)
        {
            liberacao.AtualizaLiberacao(id, value);
        }
    }

This is the method that the service calls and updates in BD:

[Route("atualiza/{id}/{value}")]
        public void AtualizaLiberacao(int id, string value)
        {
            var lista = contexto.Liberacoes
                        .Where(l => l.IdOrcamento == id).ToList();

            lista.ForEach(f =>
            {
                f.FlagLiberacao = 0;
                f.AutorizouReceberAtrazado = value;
            });

            contexto.SaveChanges();

        }

Is giving ERROR:

page not found(404)

EDIT2

The above error no longer has, but gives this error(Postman):

{ "Message": "The requested resource does not support the http 'GET' method'." }

So was my service and method

[RoutePrefix("api/Atualiza")]
    public class AtualizaController : ApiController
    {
        AutorizadorContext contexto = new AutorizadorContext();
        PedidoLiberacao liberacao = new PedidoLiberacao();

        [Route("{id}/{value}")]
        [AcceptVerbs("Put")]
        public void putItensLiberacao(int id, [FromBody]string value)
        {
            liberacao.AtualizaLiberacao(id, value);
        }
    }

and the method

[Route("atualiza/{id}/{value}")]
        public void AtualizaLiberacao(int id, string value)
        {
            var lista = contexto.Liberacoes
                        .Where(l => l.IdOrcamento == id).ToList();

            lista.ForEach(f =>
            {
                f.FlagLiberacao = 0;
                f.AutorizouReceberAtrazado = value;
            });
            contexto.SaveChanges();
        }

In the method I commented the attribute, but the error persists. I put a break point in the service and did not even enter.

  • Put in your Controller class (above the setting) [RoutePrefix("api/")] doing you a favor.

1 answer

4


It turns out that you are using the definition of routes by attributes and in this way you need to define the complete route service (this includes the api/).

You can use the attribute RoutePrefix so you don’t have to repeat the default part of the routes

[RoutePrefix("api/Atualiza")]
public class AtualizaController : ApiController
{
    [Route("{id}/{value}")]
    public void AtualizaLiberacao(int id, string value) { }
}
  • The lambda is giving dick, the same problem that I could not solve long ago, at the beginning of everything. In lambda gives me this mistake:The 'DataLib' property on 'Liberacao' could not be set to a 'System.Double' value. You must set this property to a non-null value of type 'System.Single'.

  • @pnet But this is not to do with the problem of the question, right?

  • Yeah, that’s right. I’ll do another post, in case I don’t find a solution. It’s the habit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.