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

后端 未结 11 739
傲寒
傲寒 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.

提交回复
热议问题