简体   繁体   English

请求的资源不支持 HTTP 方法“GET”

[英]The requested resource does not support HTTP method 'GET'

My route is correctly configured, and my methods have the decorated tag.我的路线配置正确,我的方法有装饰标签。 I still get "The requested resource does not support HTTP method 'GET'" message?我仍然收到“请求的资源不支持 HTTP 方法‘GET’”消息?

[System.Web.Mvc.AcceptVerbs("GET", "POST")]
[System.Web.Mvc.HttpGet]
public string Auth(string username, string password)
{
  // Décoder les paramètres reçue.
  string decodedUsername = username.DecodeFromBase64();
  string decodedPassword = password.DecodeFromBase64();

  return "value";
}

Here are my routes:以下是我的路线:

config.Routes.MapHttpRoute(
    name: "AuthentificateRoute",
    routeTemplate: "api/game/authentificate;{username};{password}",
    defaults: new { controller = "Game",
                    action = "Auth", 
                    username = RouteParameter.Optional, 
                    password = RouteParameter.Optional },
    constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { controller = "Home", id = RouteParameter.Optional }
);

Please use the attributes from the System.Web.请使用 System.Web 中的属性。 Http namespace on your WebAPI actions: WebAPI 操作上的Http命名空间:

    [System.Web.Http.AcceptVerbs("GET", "POST")]
    [System.Web.Http.HttpGet]
    public string Auth(string username, string password)
    {...}

The reason why it doesn't work is because you were using the attributes that are from the MVC namespace System.Web.Mvc .它不起作用的原因是因为您使用了来自MVC命名空间System.Web.Mvc的属性。 The classes in the System.Web.Http namespace are for WebAPI . System.Web.Http命名空间中的类用于WebAPI

In my case, the route signature was different from the method parameter.就我而言,路由签名与方法参数不同。 I had id, but I was accepting documentId as parameter, that caused the problem.我有 id,但我接受 documentId 作为参数,这导致了问题。

[Route("Documents/{id}")]   <--- caused the webapi error
[Route("Documents/{documentId}")] <-- solved
public Document Get(string documentId)
{
  ..
}

just use this attribute只需使用此属性

[System.Web.Http.HttpGet]

not need this line of code:不需要这行代码:

[System.Web.Http.AcceptVerbs("GET", "POST")]

I was experiencing the same issue.. I already had 4 controllers going and working just fine but when I added this one it returned "The requested resource does not support HTTP method 'GET'".我遇到了同样的问题。我已经有 4 个控制器可以正常工作,但是当我添加这个控制器时,它返回“请求的资源不支持 HTTP 方法‘GET’”。 I tried everything here and in a couple other relevant articles but was indifferent to the solution since, as Dan B. mentioned in response to the answer, I already had others working fine.我在这里和其他几篇相关文章中尝试了所有方法,但对解决方案无动于衷,因为正如丹 B. 在回答答案时提到的那样,我已经让其他人工作正常。

I walked away for a while, came back, and immediately realized that when I added the Controller it was nested under the "Controller" class and not "ApiController" class that my other Controllers were under.我走了一会儿,回来后立即意识到,当我添加控制器时,它嵌套在“Controller”类下,而不是我的其他控制器所在的“ApiController”类下。 I'm assuming I chose the wrong scaffolding option to build the .cs file in Visual Studio.我假设我选择了错误的脚手架选项来在 Visual Studio 中构建 .cs 文件。 So I included the System.Web.Http namespace, changed the parent class, and everything works without the additional attributes or routing.所以我包含了 System.Web.Http 命名空间,更改了父类,一切都可以在没有附加属性或路由的情况下工作。

Resolved this issue by using http(s) when accessing the endpoint.通过在访问端点时使用http(s)解决了此问题。 The route I was accessing was not available over http .我访问的路由无法通过http访问。 So I would say verify the protocols for which the route is available.所以我会说验证路由可用的协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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