Search code examples
c#asp.net-web-apiasp.net-web-api-routing

The requested resource does not support http method 'POST' with proper decoration - Web API


I have looked over a ton of 'method not supported' errors on StackOverflow but none of the suggested solutions have worked for me. I have a very basic Web API with a web method that on Post request always returns the error

'The requested resource does not support http method 'POST''

when called from PostMan with a POST request.

There are no httpprotocols defined in my web.config. I have tried with and without the Route and also without the [FromBody] in the parameter passed but all permutations have failed.

Request = http://myURL/api/XMLInput, the body of the request has an XML which I am processing.

Controller

[Route("XMLInput")]
[System.Web.Http.HttpPost]
public IHttpActionResult PostXMLInput([FromBody] XmlDocument xml)
{
    XMLInput xmlInput = new XMLInput();
    xmlInput.XML = xml.InnerXml;
    return null;
}

WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

Solution

  • The provided Request http://myURL/api/XMLInput would not match your action unless the action has a route that maps correctly

    [RoutePrefix("api")]
    public class MyController : ApiController {
    
        //POST api/XMLInput
        [Route("XMLInput")]
        [HttpPost]
        public IHttpActionResult PostXMLInput([FromBody] XmlDocument xml) { ... }
    }
    

    OR

    public class MyController : ApiController {
    
        //POST api/XMLInput
        [Route("api/XMLInput")]
        [HttpPost]
        public IHttpActionResult PostXMLInput([FromBody] XmlDocument xml) { ... }
    }
    

    Source: Attribute Routing in ASP.NET Web API 2