简体   繁体   中英

Why is my controller action returning 404?

I have a controller action called by an ajax call and it keeps returning a 404 error.

My Action:

[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 call:

$.ajax({
    url: "/TIB/Forms/GetGtmGrid",
    type: "GET",
    dataType: "json",
    //Pass parameters to controller
    data: { id: id }
})

Here is the 404 error link:

GET https://localhost:<port>/TIB/Forms/GetGtmGrid?id=428 404

Any help or suggestions are appreciated. My controller actions and ajax calls are set up like all my other ones and they work just fine.

I figured out what was causing the issue. It was the routing, ("~/TIB/Forms/GetGtmGrid"), in the action verb I was using with my controller action. So I changed my action verb from [HttpGet("~/TIB/Forms/GetGtmGrid")] to [HttpGet] and it worked as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM