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

ASP.NET Web API REST Service: HTTP ERROR 405


I have an ASP.NET Web API REST Service. It has been build using standard ASP.NET Framework 4.5 and Web API 2.2 (version 5.2.3) on Visual Studio 2013 IDE.

Using Fiddler tool I am trying to check the api methods but I get http error 405. Below some screenshots from Fiddler:

Headers tab information:

enter image description here

Raw tab information: enter image description here

Basically it says:

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

Below some code snippet from my ASP.NET Web API REST Service.

WebApiConfig.cs :

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 }
        );

        config.Routes.MapHttpRoute(
            name: "WarehouseApi",
            routeTemplate: "api/{controller}/{action}/{data}"
        );
    }
}

Controller:

[RoutePrefix("api/Warehouse")]
public class WarehouseController : ApiController
{ 
    public HttpResponseMessage GetOkResponse()
    {
         return Request.CreateResponse(HttpStatusCode.OK);            
    }

    [HttpPost]
    [Route("DumpIntoFile/{data}")]                
    public HttpResponseMessage DumpIntoFile(string data)
    {
           // Stuff here
    }
}

To test it using Fiddler, from the composer I do:

enter image description here

However If I call GetOkResponse action method from Fiddler using GET method and url: wstestDumpFile.my.domain/api/Warehouse then it works.


Solution

  • Change from this: [Route("DumpIntoFile/{data}")] to [Route("DumpIntoFile")], you are passing the data as a post and therefore are not passing the data as a route parameter. Then you can add: [FromBody] to your method parameter like: ([FromBody] string data)