【问题标题】:ASP.NET Web API - GET HTTP Verb not supported (405) when attempting to pass a parameterASP.NET Web API - 尝试传递参数时不支持 GET HTTP Verb (405)
【发布时间】:2019-02-19 07:45:56
【问题描述】:

我正在尝试使用 getJSON 请求调用我的 Web API:

var uri = 'api/comment';
var id = solicitorId;

$.getJSON(uri, id, (function (data) {
    $('#commentTableContainer').html(data);
}));

这是注释Controller类中的方法:

public string GetComment(int id)
{
    //Do things
}

我使用的是默认路由:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { action = "Index", id = UrlParameter.Optional }
            );

但是,当尝试使用 getJSON 调用 api 时,我收到 405 错误:

HTTP405: BAD METHOD - The HTTP verb used is not supported.
(XHR)GET - http://localhost:<port>/api/comment?334203

如果我从 GetComment 签名中删除 id 参数,则 GET 请求有效,即 GetComment()

我对这个 WebAPI 的东西不太了解 - 我主要遵循微软的指南,这里是 here (docs.microsoft.com)

如果有人有任何想法,我将不胜感激。我已经查看了很多关于此的 SO 问题,但没有任何帮助。

我尝试将 [HTTPGet] 添加到 CommentController GetComment(int id) 方法,以及使用 [Route] 指定路线,但我一无所获。

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 您显示的路由映射不应映射api/comment,除非您有名为api 的控制器。
  • 我希望 URI 应该是 http://localhost:&lt;port&gt;/api/comment?id=334203(注意 id=)。或者只是 http://localhost:&lt;port&gt;/api/comment/334203/ 而不是 ?,这取决于这里实际使用的路由。

标签: c# asp.net asp.net-web-api asp.net-web-api2 asp.net-web-api-routing


【解决方案1】:

您的路线配置可能与您的 URL 不匹配。

路由配置:url: "{controller}/{action}/{id}"

请求网址:/api/comment?334203

您可以尝试添加Route 属性来为您的API 操作设置RouteAttribute

[Route("api/comment/{id}")]
[HttpGet]
public string GetComment(int id)
{
    //Do things
}

你需要在你的 ajax 请求中使用完整的 url。

var uri = 'http://localhost:<port>/api/comment/GetComment';

可以匹配您的路线设置。

【讨论】:

  • 感谢您的帮助。它确实改变了一些事情,我现在得到一个 404:HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier).,这至少是不同的东西
  • @MarcosFoster 对不起,我编辑了我的答案,我忘记了参数
  • 很遗憾没有,仍然得到相同的 404。
  • 请求 URL 怎么样,/api/comment?id=334203
  • @MarcosFoster 你可以尝试在你的请求 URL 中设置完整的 URL(包括端口号)
【解决方案2】:

只需在浏览器中尝试 url =http://localhost:/api/comment/GetComment?334203。也许您缺少 URL 中的方法名称。

此外,webApi.Config 用于 Web API。如果它不起作用,请告诉我。

【讨论】:

  • 如果我这样做,我会得到:The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String GetComment(Int32)',这很有趣。使参数可以为空(GetComment(int? id))意味着该方法实际上被命中,但没有提供 ID
  • 在这种情况下,您现在应该将? 更改为/。如果还是不行,把它改成?id=334203(我不能确定你的路由实际上是如何工作的)。
【解决方案3】:

非常感谢您的帮助。 我现在已经修好了。我真的不知道该如何描述,但我将$.getJSON 调用改为:

var uri = 'api/comment/' + id;
$.getJSON(uri, (function (data) {
    //immaterial things
}

我的 CommentController 方法只返回 public string GetComment(int id) 没有 [Route][HTTPGet]

现在它可以工作了。我完全以为我以前试过这个,但没有。如果没有大家的建议,我将无法解决此问题,非常感谢您,祝您周末愉快!

【讨论】:

    【解决方案4】:

    首先更改 WebApiConfig 文件并编辑默认路由,如下所示。

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

    当你更改路由模板并添加“{action}”参数时,这意味着你要调用动作时必须添加动作名称

    然后你可以调用如下url的函数

    var uri = 'http://localhost:<port>/api/comment/GetComment/'+id;
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-19
      • 2012-06-15
      • 2016-11-26
      • 2012-12-14
      • 2015-03-10
      • 2015-04-23
      • 2016-04-14
      • 2014-06-23
      相关资源
      最近更新 更多