I have not done FrameWork Web API for a while. However... How does Asp.Net match this:
"Message": "The requested resource does not support http method 'POST'."
to this?
public IHttpActionResult PostString(string l)
Try changing to:
public IHttpActionResult PostString(string message)
UPDATE
I fired up a test project. I set a breakpoint inside the
PostString
(POST) method. I hit it, and as mentioned above, the method is hit but param
l
is null. If I change param
l
to
message
, I can now see the value passed, in this case:
The requested resource does not support http method 'POST'."
UPDATE
When passing any (JSON in this case) body data as parameters with the inherited
ApiController
, you need to specify the
[FromBody]
attribute for each parameter.
For Attribute Routing, use this:
1.
WebApiConfig
- comment out the routing.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
}
}
2. Add a
TestController
class
[RoutePrefix("api/test")]
public class TestController : ApiController
{
[HttpPost]
[Route("poststring")]
public IHttpActionResult PostString([FromBody] string message)
{
return Ok(message);
}
}
3. Run PostMan and point to:
https://localhost:44344/api/test/poststring
For Normal routing, use the following:
1.
WebApiConfig
- set routing template
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
2.
TestController
{
public class TestController : ApiController
{
[HttpPost]
public IHttpActionResult PostString([FromBody] string message)
{
return Ok(message);
}
}
3. Run PostMan and point to:
https://localhost:44344/api/test/poststring