Service does not execute controller method

Asked

Viewed 1,247 times

0

This message I get in the browser or Postman

"Message": "No HTTP resources matching URI of request 'http://localhost:9078/api/items/1000012105'.", "Messagedetail": "No action found in 'Items' controller that matches the request."

The problem is that I am building a service where I pass an ID to the url so that only the items from that budget come in. In the controller it looks like this:

public class ItensController : ApiController
    {
        AutorizadorContext contexto = new AutorizadorContext();
        ItensLiberacao itens = new ItensLiberacao();

        [AcceptVerbs("Get")]
        public IEnumerable<ItensLibDTO> getItensLiberacao(int idorcamento)
        {
            return itens.getItensLib(idorcamento).AsEnumerable().ToList();
        }
    }

And here the class Itensliberacao:

public class ItensLiberacao
    {
        AutorizadorContext contexto = new AutorizadorContext();
        ItensLibDTO libDTO = new ItensLibDTO();

        public List<ItensLibDTO> getItensLib(int idorcamento)
        {
            var lista = contexto.ItensLibs
                .Where(itens => itens.IdOrcamento == idorcamento)
                .Select(item => new ItensLibDTO
                {
                    Produto = item.Produto,
                    Qtde = item.Qtde.ToString(),
                    Unitario = item.Unitario.ToString(),
                    Custo = item.Custo.ToString(),
                    CustoDiario = item.CustoDiario.ToString(),
                    UltCondicao = item.UltCondicao.ToString(),
                    Total = item.Total.ToString()
                }).ToList();

            return lista;
        }
    }

How do I fix this? What else is missing for the service to work? The number: 1000012105 is the id of an existing budget in the database.

EDIT1

I changed my service to that, and it’s still the same mistake:

[AcceptVerbs("Get")]
        public HttpResponseMessage getItensLiberacao(int idorcamento)
        {
            var _itens = contexto.ItensLibs.Where(it => it.IdOrcamento == idorcamento).FirstOrDefault();
            return Request.CreateResponse(HttpStatusCode.OK, _itens);

        }

EDIT2

If I change the method and the service works, so:

public class ItensLiberacao
    {
        AutorizadorContext contexto = new AutorizadorContext();
        ItensLibDTO libDTO = new ItensLibDTO();

        public List<ItensLibDTO> getItensLib()
        {
            var lista = contexto.ItensLibs
                //.Where(itens => itens.IdOrcamento == idorcamento)
                .Select(item => new ItensLibDTO
                {
                    Produto = item.Produto,
                    Qtde = item.Qtde.ToString(),
                    Unitario = item.Unitario.ToString(),
                    Custo = item.Custo.ToString(),
                    CustoDiario = item.CustoDiario.ToString(),
                    UltCondicao = item.UltCondicao.ToString(),
                    Total = item.Total.ToString()
                }).ToList();

            return lista;
        }
    }

And the service:

public class ItensController : ApiController
    {
        AutorizadorContext contexto = new AutorizadorContext();
        ItensLiberacao itens = new ItensLiberacao();

        [AcceptVerbs("Get")]
        public IEnumerable<ItensLibDTO> getItensLiberacao()
        {
            return itens.getItensLib().AsEnumerable().ToList();
        }
    }
  • Your route is the default web api?

  • Test: http://localhost:9078/api/items/getitensrelease/1000012105 and make sure it’s a request is a GET

  • @Gabrielcoletta, still the same problem. The same error message

  • Guys, what I noticed is that it doesn’t enter the method when I call the url. I use the default route, as my colleague @Gabrielcoletta asked. I changed the method and still it doesn’t work. I am since 4:30 am trying, reading and nothing yet, so far.

  • I don’t know if it can be that, but when I change the method and the service to receive by ID, I’m bringing a list and it should be, I think, FirstOrDefault(). I think that might be it, and I’m going to test it.

  • What version of the webapi you are using?

  • @Grupocdsinformática, how I see it?

  • 1

    @pnet Checks the version of the webapi package in nuget. I asked why there is a way to do it, including the following attribute in the controller method: [Route( "itensrelease/{marking}" )] and passing this value as attribute to the controller [Routeprefix("api/items")]

  • @Grupocdsinformática, picking up by the installed packages I have the following: Microsoft.AspNet.Webapi Version 5.2.3, I don’t know if that’s it, I think so.

  • The problem is that it doesn’t even enter the method in the controller(service) and I can’t debug it. It’s totally wrong and I don’t know what it is and I have no idea

  • Sets the icon parameter to id and test only.

  • @pnet is Webapi 2.2, tests with what I’ve been through, able to solve.

  • @Thiagosilva, this way entered the method. Gave another error in the double field, but this is for another post. Thanks. Post as an answer that I mark.

  • @Grupocdsinformática, I switched from branding to id and it worked. I was without internet, so I didn’t comment before. And how I see the webapi version?

  • @pnet, I saw by the version of Assembly that passed me above, I only researched from there ;)

Show 10 more comments

3 answers

0

Your route is set so that the first parameter is always called id. If you want to pass a parameter with another name you do so: http://localhost:9078/api/itens?idorcamento=1000012105

0

You can either change the parameter to "id" or put the annotation:

[HttpGet("{idorcamento}")]    
public IEnumerable<ItensLibDTO> getItensLiberacao(int idorcamento){
...
}

-1

Dude you can try to use the route like this give a look

[HttpGet("getItensLiberacao/{idorcamento}")]
public IEnumerable<ItensLibDTO> getItensLiberacao(int idorcamento){}

Browser other questions tagged

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