我有一个由ajax调用调用的控制器操作,它一直返回404错误。
我的行动:
[HttpGet("~/TIB/Forms/GetGtmGrid")]
public IEnumerable<object> GetGtmGrid(int? id)
{
DataTable dtbl = new DataTable();
using (SqlConnection sqlConnection = new SqlConnection(_configuration.GetConnectionString("DB")))
{
sqlConnection.Open();
SqlDataAdapter sqlData = new SqlDataAdapter("usp_GetGTMNotesById", sqlConnection);
sqlData.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlData.SelectCommand.Parameters.AddWithValue("Id", id);
sqlData.Fill(dtbl);
}
List<GTMNote> gtmNotes = new List<GTMNote>();
for (int i = 0; i < dtbl.Rows.Count; i++)
{
gtmNotes.Add(new GTMNote
{
Id = Convert.ToInt32(dtbl.Rows[i]["Id"]),
Text = dtbl.Rows[i]["Text"].ToString(),
ProjectId = Convert.ToInt32(dtbl.Rows[i]["ProjectId"]),
Date = (DateTime)dtbl.Rows[0]["Date"],
StatusId = Convert.ToInt32(dtbl.Rows[i]["StatusId"])
});
}
GTMNote[] gtmArray = gtmNotes.ToArray();
return gtmArray;
}
AJAX调用:
$.ajax({
url: "/TIB/Forms/GetGtmGrid",
type: "GET",
dataType: "json",
//Pass parameters to controller
data: { id: id }
})
下面是404错误链接:
GET
https://localhost:<port>/TIB/Forms/GetGtmGrid?id=428
404
任何帮助或建议都是感激的。我的控制器动作和ajax调用设置像我所有其他的,他们工作得很好。
我找到了导致这个问题的原因。这是路由,("~/TIB/Forms/GetGtmGrid"),在我与控制器操作使用的动作动词。所以我把我的动作动词从[HttpGet("~/TIB/Forms/GetGtmGrid")]改为[HttpGet],它像预期的那样工作了。