0

I have a API Controller with a Method that recieve a parameter. When I call it from the client, get an 404 error. If a re-write de method with ni parameter it works.

Here is my API controller:

 public class ClientController : ApiController
    {

        [HttpGet]
        public List<User> GetAAA(int userCode)
        {
           return null;
        }
    }

Here is my Client...

using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(apiUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                List<User> list;
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                string uriString = string.Format("{0}/{1}", "http://localhost:7734/api/Client/GetAAA", 1783);
                HttpResponseMessage response = await client.GetAsync(uriString);
                if (response.IsSuccessStatusCode)
                    list = response.Content.ReadAsAsync<List<User>>().Result;
                else
                    throw new Exception("Error");
            }

Even if I write in navigator http://localhost:7734/api/Client/GetAAA/1783 it says "Page not found".

My WebApiConfig is like this:

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

What am I missing?

5
  • Client is misspelled in that url? Commented Oct 20, 2017 at 15:08
  • OK it is localhost:7734/api/Client/GetAAA/1783 ?? Commented Oct 20, 2017 at 15:10
  • Your url path segment needs to match the name of the controller exactly. Since your controller is named ClientController your url needs to be /api/Client/ Commented Oct 20, 2017 at 15:11
  • yes I worte it bad to this example. if I create a method with zero parameters, it Works.... Commented Oct 20, 2017 at 15:12
  • Possible duplicate of Web Api Get() route not working when return type is HttpResponseMessage Commented Oct 20, 2017 at 15:27

4 Answers 4

1

Update the controller Action to have a Route attribute as below

 public class ClientController : ApiController
 {
      [Route("api/Client/GeAAA/{userCode}")]    
      [HttpGet]
      public List<User> GetAAA(int userCode)
      {
         return null;
      }
 }

Sign up to request clarification or add additional context in comments.

Comments

1

Change your controller Method:

[HttpGet]
public List<User> GetAAA(int id)
{
    return null;
}

Or use the url:

http://localhost:7734/api/Client/GetAAA?userCode=1783

Comments

0

It looks like this is a case of Convention based routing vs Attribute routing.

ASP.NET MVC will use convention based routing, as stated in your config file.

Because of this, "GET" will be dropped from the url parameter.

it should look something like this:

http://localhost:7734/api/Client/AAA/1783

I have personally found convention based routing less useful though, so I use attribute based routing (also configured in your config file).

Add this to your GetAAA method:

[HttpGet]
[Route("GetAAA/{userCode}")]
public List<User> GetAAA(int userCode)
{
     ...
}

this Route attribute should allow you to use the link as you've stated.

2 Comments

I do not want to work with Route... There is no change to acceted as defined in WebApiConfig? I do not why it doesnot work. It Works in others projects....
web API 2.0 does some magic. If you read some documentation on it, it will tell you that "Get" is a keyword and it will drop it from a method for the api call
0

It was my mistake. Such a dumb I did not see it before.

My idea is not to use [Route], just use how is defined in WebApiConfig

the definition is

   config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

The only thing I have to do in my call

public List GetAAA(int userCode)

is replace de parameter name....

public List GetAAA(int id)

id is how is defined in the configuration...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.