Why is it that "No HTTP resource was found that matches the request URI" here?
Your problems have nothing to do with POST/GET but only with how you specify parameters in RouteAttribute
. To ensure this, I added support for both verbs in my samples.
Let's go back to two very simple working examples.
[Route("api/deliveryitems/{anyString}")]
[HttpGet, HttpPost]
public HttpResponseMessage GetDeliveryItemsOne(string anyString)
{
return Request.CreateResponse<string>(HttpStatusCode.OK, anyString);
}
And
[Route("api/deliveryitems")]
[HttpGet, HttpPost]
public HttpResponseMessage GetDeliveryItemsTwo(string anyString = "default")
{
return Request.CreateResponse<string>(HttpStatusCode.OK, anyString);
}
The first sample says that the "anyString
" is a path segment parameter (part of the URL).
First sample example URL is:
- localhost:
xxx/api/deliveryItems/dkjd;dslkf;dfk;kkklm;oeop
- returns
"dkjd;dslkf;dfk;kkklm;oeop"
- returns
The second sample says that the "anyString
" is a query string parameter (optional here since a default value has been provided, but you can make it non-optional by simply removing the default value).
Second sample examples URL are:
- localhost:
xxx/api/deliveryItems?anyString=dkjd;dslkf;dfk;kkklm;oeop
- returns
"dkjd;dslkf;dfk;kkklm;oeop"
- returns
- localhost:
xxx/api/deliveryItems
- returns
"default"
- returns
Of course, you can make it even more complex, like with this third sample:
[Route("api/deliveryitems")]
[HttpGet, HttpPost]
public HttpResponseMessage GetDeliveryItemsThree(string anyString, string anotherString = "anotherDefault")
{
return Request.CreateResponse<string>(HttpStatusCode.OK, anyString + "||" + anotherString);
}
Third sample examples URL are:
- localhost:
xxx/api/deliveryItems?anyString=dkjd;dslkf;dfk;kkklm;oeop
- returns
"dkjd;dslkf;dfk;kkklm;oeop||anotherDefault"
- returns
- localhost:
xxx/api/deliveryItems
- returns "No HTTP resource was found that matches the request URI ..." (parameter
anyString
is mandatory)
- returns "No HTTP resource was found that matches the request URI ..." (parameter
- localhost:
xxx/api/deliveryItems?anotherString=bluberb&anyString=dkjd;dslkf;dfk;kkklm;oeop
- returns
"dkjd;dslkf;dfk;kkklm;oeop||bluberb"
- note that the parameters have been reversed, which does not matter, this is not possible with "URL-style" of first example
- returns
When should you use path segment or query parameters? Some advice has already been given here: REST API Best practices: Where to put parameters?
Have you tried using the [FromUri]
attribute when sending parameters over the query string.
Here is an example:
[HttpGet]
[Route("api/department/getndeptsfromid")]
public List<Department> GetNDepartmentsFromID([FromUri]int FirstId, [FromUri] int CountToFetch)
{
return HHSService.GetNDepartmentsFromID(FirstId, CountToFetch);
}
Include this package at the top also, using System.Web.Http;
WebApiConfig.Register(GlobalConfiguration.Configuration); should be on top.