I created the simplest WebAPI 2 application using empty project template and Web API folder structure (in Visual Studio 2013). And cannot make work post request.
This is my controller
public class XmlFileController : ApiController
{
public IHttpActionResult Post(string payload)
{
return Ok();
}
public string Get()
{
return "Hello world";
}
}
I didn't change the Routes or anything. To my understanding, routing should work by name convention.
When calling GET http://localhost:51356/api/XmlFile, I get back Status 200 OK.
When calling POST http://localhost:51356/api/XmlFile with string payload and Content-Type text/plain, I got back Status 405 Method Not Allowed.
What am I missing here? Thanks.
UPDATE: The routing configuration is as follows:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Your POST is expecting a parameter - payload in query string. Without this parameter, it is looking for Post() function which is not defined, hence you are getting 405 error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With