Tristan Claude
Tristan Claude

Reputation: 51

API works fine but parameters are not accepted c#

I have my route prefix here:

[RoutePrefix("api/Adresses")]
public class AdressesController : ApiController
{

My function here:

[Route("{codeEtudiant}")]
// GET: api/Adresses/1
public IEnumerable<Object> getAdresseEtu(Int32 code)
{

Where I call my api:

using (var client2 = new HttpClient())
{
    string getasync = "http://localhost:11144/api/Adresses/" + etu.Code;
    var response2 = await client.GetAsync(getasync);
    var json2 = await response.Content.ReadAsStringAsync();
    int cpt2 = -1;
    foreach (object tmp2 in JsonConvert.DeserializeObject<List<Object>>(json2))
    {

My string getasync returns: http://localhost:11144/api/Adresses/1

With these methods I can call any function in my api that does not have parameters but as soon I have one it doesn't respond and give me a response:

404 reason(not found)

Upvotes: 3

Views: 69

Answers (2)

Igor
Igor

Reputation: 62213

The parameter names have to match. Currently you have the route parameter named codeEtudiant but the parameter of the method named code. Give them both the same name.

[Route("{codeEtudiant}")]
public IEnumerable<Object> getAdresseEtu(Int32 codeEtudiant)

See also Attribute Routing in ASP.NET Web API 2.

Upvotes: 4

Luiz Bicalho
Luiz Bicalho

Reputation: 935

The error is because the Route Attribute must have the same name of the parameter, use

[Route("{code}")]

Upvotes: 0

Related Questions