【发布时间】: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:<port>/api/comment?id=334203(注意id=)。或者只是http://localhost:<port>/api/comment/334203和/而不是?,这取决于这里实际使用的路由。
标签: c# asp.net asp.net-web-api asp.net-web-api2 asp.net-web-api-routing