C# ASP.NET Get method with parameter returning 404

Asked

Viewed 101 times

0

Good afternoon, folks, I was wondering if anyone could assist me. My Web Service has a Get method that returns the entire list of Products, however I would like to create a method that it returns ONE Product according to a code that passed to it. At the moment all the method with parameter is returning me is a 404 when tested on Fiddler. Any idea? Any methods you have? any help is welcome thanks.

Code:

[Route("api/Produto")]
public class ProdutoConsultController : ApiController
{


    // GET api/ProdutoConsult
    public ObjectResult<uspConsultarProduto_Result> Get()
    {
        ControleDeEstoqueEntities entity = new ControleDeEstoqueEntities();
        var result = entity.uspConsultarProduto(null);
        return result;
    }

    // GET api/ProdutoConsult/5
    public List<produto> Get(int cod)
    {
        ControleDeEstoqueEntities entity = new ControleDeEstoqueEntities();
        List<produto> MyList = new List<produto>();
        var result = from produto in entity.produto where produto.pro_cod == cod select produto;
        MyList.AddRange(result);

        return MyList;
    }
  • Keep the question in its initial state, otherwise it loses its meaning.

1 answer

1


The attribute Route is not being used properly.

You should use them in actions and not in controller, in the controller one should use RoutePrefix to set a "prefix" for the route.

public class ProdutoConsultController : ApiController
{
    [Route("api/Produto")]
    public ObjectResult<uspConsultarProduto_Result> Get()
    {
        // Retornar todos
    }

    [Route("api/Produto/{cod}")]
    public List<produto> Get(int cod)
    {
        // retornar um
    }
}
  • THANK YOU SO MUCH for the clarification jbueno,vc is my savior KKKK that explains why it was not working.

Browser other questions tagged

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