The way web API routes work is you need to add
config.MapHttpAttributeRoutes();
To your WebApiConfig.cs file if it isn't there.
Then as for the route you have two attributes to keep in mind, RoutePrefix and Route.
So an example of this would be
[RoutePrefix("api/people")]
public class PeopleController : Controller
{
[Route("names")]
public IHttpActionResult GetNames()
{
var names =
return Ok(names);
}
}
This example allows you to use the url http://localhost/api/people/names instead of the uglier version of http://localhost/people/getnames.
Now for actions with parameters in web api routes you'd do something like
[RoutePrefix("api/people")]
public class PeopleController : Controller
{
[Route("names/{personId}")]
public IHttpActionResult GetNameForPersion(int personId)
{
var names =
return Ok(names);
}
}
This would allow you add an ID that represents a person to the URL. Keep in mind, the parameter name in your action must be the same name used in the route. Otherwise i believe you'll get a 404 error.
So this url would be something like http://localhost/api/people/names/1
If you have any more questions here is a good reference:
Attribute Routing in ASP.NET Web API 2 | The ASP.NET Site[
^]