【发布时间】:2020-02-15 05:23:20
【问题描述】:
我在 Asp.Net Core 中尝试将 Ajax 调用中的数据传递到 API 控制器函数的参数时遇到问题。
我正在使用 Ajax 调用中的数据字段来传递“id”的值。在 API 控制器中,该值应分配给“id”参数,但绝不是。
// Ajax call
$.ajax({
url: "/api/apicomment/GetPostComments",
type: "GET",
data: { 'id' : 2 },
dataType: "json",
}).done(function (data) {
// Some function
}).fail(function (handleError) {
// Some function
});
API 控制器是一个普通的脚手架 API 控制器,我可以在其中使用 id 参数获取特定的 cmets。但是每次我拨打电话时,我都会得到 0 的值。
// API Controller
[HttpGet("{id}")]
[Route("GetPostComments")]
public async Task<ActionResult<IEnumerable<Comment>>> GetSpecificComment(int id)
{
var comment = await _context.Comment.Where(c => c.PostId == id).ToListAsync();
if (comment == null)
{
return NotFound();
}
return comment;
}
尝试了很多不同的东西,但我无法完全弄清楚。
希望有任何可能有帮助的反馈!
【问题讨论】:
标签: c# ajax asp.net-core .net-core asp.net-core-mvc