ASP.NET Web Api : 요청 된 리소스가 http 메서드 'GET'을 지원하지 않습니다.
ApiController에 다음 작업이 있습니다.
public string Something()
{
return "value";
}
그리고 다음과 같이 내 경로를 구성했습니다.
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
베타에서는 잘 작동했지만 최신 릴리스 후보로 업데이트했으며 이제 다음과 같은 호출에 오류가 표시됩니다.
요청 된 리소스는 http 메소드 'GET'을 지원하지 않습니다.
왜 더 이상 작동하지 않습니까?
(내가 {action}을 제거하고 많은 컨트롤러를 만들 수 있다고 생각하지만 지저분하다.)
컨트롤러의 작업에 대해 HttpMethod를 구성하지 않은 경우 RC의 HttpPost 만있는 것으로 간주됩니다. 베타에서는 GET, PUT, POST 및 Delete와 같은 모든 방법을 지원한다고 가정합니다. 이것은 베타에서 RC 로의 작은 변경 사항입니다. [AcceptVerbs ( "GET", "POST")]를 사용하여 작업에 대해 하나 이상의 httpmethod를 쉽게 장식 할 수 있습니다.
위의 모든 정보는 정확합니다. 또한 [AcceptVerbs()]
주석이 System.Web.Mvc 및 System.Web.Http 네임 스페이스에 모두 존재 한다는 점을 지적하고 싶습니다 .
Web API 컨트롤러 인 경우 System.Web.Http를 사용하려고합니다.
이것이 OP에 대한 답은 아니지만 완전히 다른 근본 원인에서 똑같은 오류가 발생했습니다. 다른 사람에게 도움이된다면 ...
나에게 문제는 WebAPI가 예기치 않게 요청을 라우팅하게 만드는 잘못된 이름의 메서드 매개 변수였습니다. 내 ProgrammesController에 다음 메서드가 있습니다.
[HttpGet]
public Programme GetProgrammeById(int id)
{
...
}
[HttpDelete]
public bool DeleteProgramme(int programmeId)
{
...
}
... / api / programmes / 3에 대한 DELETE 요청은 내가 예상 한대로 DeleteProgramme로 라우팅되지 않고 GetProgrammeById로 라우팅됩니다. DeleteProgramme에 id라는 매개 변수 이름이 없기 때문입니다. GetProgrammeById는 물론 GET 만 허용하는 것으로 표시되어 DELETE를 거부했습니다.
따라서 수정은 간단했습니다.
[HttpDelete]
public bool DeleteProgramme(int id)
{
...
}
그리고 모든 것이 좋습니다. 정말 어리석은 실수이지만 디버그하기는 어렵습니다.
으로 메소드를 장식 하는 경우 컨트롤러 상단에 HttpGet
다음 using
을 추가합니다 .
using System.Web.Http;
를 사용하는 System.Web.Mvc
경우이 문제가 발생할 수 있습니다.
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.
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.
Have the same Setup as OP. One controller with many actions... less "messy" :-)
In my case i forgot the "[HttpGet]" when adding a new action.
[HttpGet]
public IEnumerable<string> TestApiCall()
{
return new string[] { "aa", "bb" };
}
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 }
);
I don't know if this can be related to the OP's post but I was missing the [HttpGet] annotation and that was what was causing the error, as stated by @dinesh_ravva methods are assumed to be HttpPost by default.
My issue was as simple as having a null reference that didn't show up in the returned message, I had to debug my API to see it.
'IT story' 카테고리의 다른 글
비활성화 된 입력 필드의 값 제출 (0) | 2020.09.08 |
---|---|
SQL에서 StringBuilder를 사용하는 올바른 방법 (0) | 2020.09.08 |
Django 클래스 기반보기 : as_view 메소드에 추가 매개 변수를 어떻게 전달합니까? (0) | 2020.09.08 |
BOM으로 UTF-8 파일을 검색하는 우아한 방법? (0) | 2020.09.08 |
Sublime 2 업데이트 알림을 끄는 방법은 무엇입니까? (0) | 2020.09.08 |