简体   繁体   中英

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:

在此处输入图像描述

Raw tab information: 在此处输入图像描述

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:

在此处输入图像描述

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM