ASP.NET Web Api:请求的资源不支持http方法'GET'

Jos*_*ltz 90 asp.net asp.net-web-api

我在ApiController上有以下动作:

public string Something()
{
    return "value";
}
Run Code Online (Sandbox Code Playgroud)

我已按如下方式配置了我的路线:

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)

在测试版中,这工作得很好,但我刚刚更新到最新的Release Candidate,现在我看到这样的调用出错了:

请求的资源不支持http方法'GET'.

为什么这不再工作了?

(我想我可以摆脱{action}而只是制造大量的控制器,但这感觉很麻烦.)

小智 107

如果你没有在控制器中的动作上配置任何HttpMethod,则假定它只是RC中的HttpPost.在Beta中,假设它支持所有方法 - GET,PUT,POST和Delete.这是从beta到RC的一个小变化.您可以使用[AcceptVerbs("GET","POST")]在您的操作上轻松解析多个httpmethod.

  • @Josh:是的!当action方法的名称以"Get ..."开头时,您不必将其标记为GET方法.在这里阅读更多内容:http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api (3认同)

Eri*_*ric 53

以上所有信息都是正确的,我还想指出[AcceptVerbs()]注释存在于System.Web.Mvc和System.Web.Http命名空间中.

如果它是Web API控制器,您希望使用System.Web.Http.


Car*_*man 34

虽然这不是OP的答案,但是我从完全不同的根本原因得到完全相同的错误; 所以如果这有助于其他人......

对我来说问题是一个错误命名的方法参数,导致WebAPI意外地路由请求.我的ProgrammesController中有以下方法:

[HttpGet]
public Programme GetProgrammeById(int id)
{
    ...
}

[HttpDelete]
public bool DeleteProgramme(int programmeId)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

DELETE对.../api/programs/3的请求没有像我预期的那样被路由到DeleteProgramme,而是被GetProgrammeById,因为DeleteProgramme没有id的参数名.GetProgrammeById当然拒绝DELETE,因为它被标记为只接受GET.

所以修复很简单:

[HttpDelete]
public bool DeleteProgramme(int id)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

一切都很好.愚蠢的错误真的很难调试.


Soh*_*N3N 19

如果要使用装饰方法HttpGet,请using在控制器顶部添加以下内容:

using System.Web.Http;
Run Code Online (Sandbox Code Playgroud)

如果您正在使用System.Web.Mvc,则可能会出现此问题.

  • 这是事实,并且可笑的.NET没有清楚地显示消息. (4认同)

Jer*_*emy 14

这肯定是从Beta到RC的变化.在问题中提供的示例中,您现在需要使用[HttpGet]或[AcceptVerbs("GET")]来装饰您的操作.

如果您想要将基于动词的动作(即"GetSomething","PostSomething")与基于非动词的动作混合,则会出现问题.如果您尝试使用上述属性,则会导致与控制器中任何基于动词的操作发生冲突.获取arount的一种方法是为每个动词定义单独的路由,并将默认动作设置为动词的名称.此方法可用于在API中定义子资源.例如,以下代码支持:"/ resource/id/children"其中id和children是可选的.

        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") }
        );
Run Code Online (Sandbox Code Playgroud)

希望未来版本的Web API能够更好地支持这种情况.目前在aspnetwebstack codeplex项目上记录了一个问题,http: //aspnetwebstack.codeplex.com/workitem/184 .如果您希望看到这些内容,请就此问题进行投票.


Ver*_* D. 6

与上述相同的问题,但根本不同的根源。对我来说,这是我使用 https 重写规则访问端点。在 http 上点击它会导致错误,在 https 上按预期工作。


Sha*_*kir 5

与OP具有相同的设置。一个具有许多动作的控制器...更少的“混乱” :-)

就我而言,添加新操作时我忘记了“ [HttpGet]”。

[HttpGet]
public IEnumerable<string> TestApiCall()
{
    return new string[] { "aa", "bb" };
}
Run Code Online (Sandbox Code Playgroud)