我正在做一个简单的get调用,但是我似乎无法在我的Web API控制器中得到正确的get方法来触发。我的路线如下:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
// I already tried removing this, same result
routes.MapSpaFallbackRoute("spa-fallback",
new { controller = "Home", action = "Index" });
});
以及包含GET方法的控制器(按顺序):
[Route("api/[controller]")]
[ApiController]
public class SchoolYearsController : ControllerBase
{
[HttpGet("~/api/terms/{termId:int}")]
public IActionResult GetTermById(int termId)
{
// omitted for brevity
}
[HttpGet("~/api/terms")]
public IActionResult GetAllTerms()
{
// omitted for brevity
}
}
我的ajax调用:
loadTerm() {
$.ajax({
url: "/api/terms?termid=3",
method: "GET",
contentType: "application/json; charset=utf-8"
})
.done((response, textStatus, xhr) => {
// omitted for brevity
})
.fail((xhr, textStatus, errorThrown) => {
console.log(errorThrown);
});
}
我想这些是相关的章节。出于某种原因,它总是调用第二个方法(“~/api/terms”),而不是使用termId参数的方法。有什么想法吗?