I'm trying to add a special route to the default WebApi sample:
The regular ones are
/api/values
(retrieves all values)
/api/values/{id}
(retrieves a specific value (by numeric id))
Now I want to add this api:
/api/values/special
The previous api (/api/values/{id}
) should serve all requests with a numeric id, but /api/values/special
should serve requests that call that specific url.
So far I got this for routing:
config.Routes.MapHttpRoute("SpecialValues", "api/values/special", new { controller = "values", action = "SpecialValues" });
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
And this for implementation:
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
// GET api/values/special
public IEnumerable<string> SpecialValues()
{
return new string[] { "special1", "special2" };
}
}
But it will render: The requested resource does not support http method 'GET'
.
If I call /api/values/special
and I add [HttpGet]
to the SpecialValues
method it will work
but /api/values
will stop working saying that there are multiple matching actions.