I am trying trying to call my web api.
This is the api controller:
public class CustomerController : ApiController
{
[HttpGet]
[Route("Customer/Get/{CompanyRef}")]
public IEnumerable<Services.Customer> Get(Guid CompanyRef)
{
return customerRepository.Get(CompanyRef);
}
}
This is my client (c#desktop)
using (HttpClient httpClient = new HttpClient())
{
Uri uri = new Uri("myuri");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await httpClient.GetAsync(uri + route + "?CompanyRef=" + new Guid());
response.EnsureSuccessStatusCode();
}
the uri call translates to:
http://myuri/api/Customer/Get?CompanyRef=00000000-0000-0000-0000-000000000000
the error I get is 'not found'?
The accepted answer is actually completely in the wrong when it comes to OP's code and question. That is ASP.net WEB API v1.x way of doing the routing and OP is right in his comment that it is useless when using attributes.
[v2+ of Web API required] For Attributes to work and the routes to be registered, you need to add the following code in your WebApiConfig.cs Route(config) method:
config.MapHttpAttributeRoutes();
That will parse all your [RoutePrefix("..")]
and [Route("..")]
and create your API's routing. It is a best practice to use [RoutePrefix("..")]
to define the general path of the API in order to reach your controller & then map your different methods using the [Route("..")]
& http verb attribute such as [HttpGet]
.