1

Working in ASP.NET 4.6 here.

I have a controller:

public class ComputerController : ApiController
{
    ...

    [HttpGet]
    [Route("api/computer/ping")]
    public IHttpActionResult Ping(int id)
    {
        return Ok("hello");
    }

    ...
}

Going mostly from this answer (look at MSTdev's answer), I have this in my WebApiConfig.cs:

// So I can use [Route]?
config.MapHttpAttributeRoutes();
// handle the defaults.
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

The route doesn't work. I always get

No HTTP resource was found that matches the request URI
'http://localhost:29365/api/computer/ping'.

This seems like such a simple problem, yet I remain stumped. Any help?

Community
  • 1
  • 1
crowhill
  • 2,398
  • 3
  • 26
  • 55

1 Answers1

3

Your route is missing the {id} parameter. Ex.

[Route("api/category/{categoryId}")]
public IEnumerable<Order> GetCategoryId(int categoryId) { ... }

Your controller should look like this:

public class ComputerController : ApiController
{
    ...

    [HttpGet]
    [Route("api/computer/ping/{id}")]
    public IHttpActionResult Ping(int id)
    {
        return Ok("hello");
    }

    ...
}
Jesus Angulo
  • 2,646
  • 22
  • 28