ASP.NET Web Api: The requested resource does not support http method 'GET'

后端 未结 11 663
傲寒
傲寒 2020-12-04 18:50

I\'ve got the following action on an ApiController:

public string Something()
{
    return \"value\";
}

And I\'ve configured my routes as f

相关标签:
11条回答
  • 2020-12-04 19:21

    This is certainly a change from Beta to RC. In the example provided in the question, you now need to decorate your action with [HttpGet] or [AcceptVerbs("GET")].

    This causes a problem if you want to mix verb based actions (i.e. "GetSomething", "PostSomething") with non verb based actions. If you try to use the attributes above, it will cause a conflict with any verb based action in your controller. One way to get arount that would be to define separate routes for each verb, and set the default action to the name of the verb. This approach can be used for defining child resources in your API. For example, the following code supports: "/resource/id/children" where id and children are optional.

            context.Routes.MapHttpRoute(
               name: "Api_Get",
               routeTemplate: "{controller}/{id}/{action}",
               defaults: new { id = RouteParameter.Optional, action = "Get" },
               constraints: new { httpMethod = new HttpMethodConstraint("GET") }
            );
    
            context.Routes.MapHttpRoute(
               name: "Api_Post",
               routeTemplate: "{controller}/{id}/{action}",
               defaults: new { id = RouteParameter.Optional, action = "Post" },
               constraints: new { httpMethod = new HttpMethodConstraint("POST") }
            );
    

    Hopefully future versions of Web API will have better support for this scenario. There is currently an issue logged on the aspnetwebstack codeplex project, http://aspnetwebstack.codeplex.com/workitem/184. If this is something you would like to see, please vote on the issue.

    0 讨论(0)
  • 2020-12-04 19:22

    Replace the following code in this path

    Path :

    App_Start => WebApiConfig.cs

    Code:

    config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}/{Param}",
                defaults: new { id = RouteParameter.Optional,
                                Param = RouteParameter.Optional }
                              );
    
    0 讨论(0)
  • 2020-12-04 19:25

    Same problem as above, but vastly different root. For me, it was that I was hitting an endpoint with an https rewrite rule. Hitting it on http caused the error, worked as expected with https.

    0 讨论(0)
  • 2020-12-04 19:25

    I got this error when running a query without SSL.

    Simply changing the URL scheme of my request from HTTP to HTTPS fixed it.

    0 讨论(0)
  • 2020-12-04 19:30

    If you are decorating your method with HttpGet, add the following using at the top of the controller:

    using System.Web.Http;
    

    If you are using System.Web.Mvc, then this problem can occur.

    0 讨论(0)
提交回复
热议问题