简体   繁体   English

为什么我的控制器操作返回 404?

[英]Why is my controller action returning 404?

I have a controller action called by an ajax call and it keeps returning a 404 error.我有一个由 ajax 调用调用的控制器操作,它不断返回 404 错误。

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 调用:

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

Here is the 404 error link:这是404错误链接:

GET https://localhost:<port>/TIB/Forms/GetGtmGrid?id=428 404获取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.我的控制器操作和 ajax 调用的设置与我所有其他的一样,它们工作得很好。

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.这是我在控制器动作中使用的动作动词中的路由(“~/TIB/Forms/GetGtmGrid”)。 So I changed my action verb from [HttpGet("~/TIB/Forms/GetGtmGrid")] to [HttpGet] and it worked as expected.所以我将我的动作动词从 [HttpGet("~/TIB/Forms/GetGtmGrid")] 更改为 [HttpGet] 并且它按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我从 MVC controller 下载的文件返回 HTTP 404 - Why is my file download from MVC controller returning HTTP 404 Rails 4:找不到对控制器操作的ajax调用,返回404 - Rails 4: ajax call to controller action returning 404 not found 为什么我的路线返回404错误Laravel? - Why is my route returning 404 error Laravel? 为什么我的 API 中的这个端点返回 [] 而不是 404 错误? - Why is this endpoint in my API returning [] and not 404 error? 使用Javascript(Jquery)调用MVC3 Controller / Action / ID的URL路由问题,返回404,应获取JSon - URL Routing issue with Javascript (Jquery) call to MVC3 Controller/Action/ID, returning 404, should get JSon Controller 返回 404 - 节点 JS - Controller returning 404 - Node JS 为什么我的 controller 操作添加 2 条新记录而不是 1 条 - Why does my controller action add 2 new records instead of one 我的控制器动作出现路由问题 - Routing issue with my controller action Web API路线404:“在匹配的控制器上未找到任何动作” - Web API Route 404: “No action was found on the controller that matches” 部署到远程服务器后控制器操作方法中的 404 错误 - 404 Error in controller action methods after deploying to remote server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM