C # ASP.NET Method Get with parameter returning 404

0

Good afternoon guys, I'd like to know if anyone can help me. My Web Service has a Get method that returns the entire List of Products, however I would like to create a method that returns a Product according to a code passed to it. At the moment all that the method with parameter is returning me is a 404 when tested on Fiddler. Any idea? Any method 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;
    }
    
asked by anonymous 03.12.2016 / 20:50

1 answer

1

The Route attribute is not being used correctly.

You must use them in the actions and not in the controller, in the controller you must use RoutePrefix to define a "prefix" for the route. p>

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
    }
}
    
03.12.2016 / 20:55