C# 如何检索和获取参数

C# 如何检索和获取参数,c#,jquery,asp.net-core,C#,Jquery,Asp.net Core,检索HttpGet参数 我有这样的方法 [HttpGet("SendEmails/sendStudentMailIR/{studentID}/{email}/{surID}")] public async Task<bool> sendStudentMailIR(string studentID, string email, string surID) { bool isSent = fals

检索HttpGet参数

我有这样的方法

        [HttpGet("SendEmails/sendStudentMailIR/{studentID}/{email}/{surID}")]
        public async Task<bool> sendStudentMailIR(string studentID, string email, string surID)
        {
            bool isSent = false;
            ...
            return isSent;
        }

但我从来没有达到的方法。我错过了什么。核心3.1就是我正在使用的。

应该是
[HttpPost(“sendmails/sendStudentMailIR/{studentID}/{email}/{surID}”)]
因为您在ajax请求中使用的是类型:“POST”

假设您想保持控制器方法的原样,一个
HttpGet
请求,您需要更改
ajax
,请更改为以下内容:

$.ajax({
            url: "/SendEmails/sendStudentMailIR/" + studentID + "/" + email + "/" + surID,
            //data: JSON.stringify(ko.toJSON(this)), no need to set data as they are passed in the url
            type: 'GET', //You have a controller method of Get not Post
            dataType: 'json',
            contentType: 'application/json',
            cache: false, //don't cache this request
            success: function (data) {
                alert("Send");
            },
            error: function(jqXHR, exception){ //Added error for exception handling

            }
        });

当您的
ajax
尝试执行“Post”时,您的控制器方法被标记为“Get”,将
ajax
更改为
类型:'Get'
,而且,我认为您不需要传递
数据,因为参数正在
url
中传递,在
[HttpGet()]
属性中添加
[FromQuery]
,我认为这应该是在查询字符串中提取参数的信号。@janzen它们不能来自其他任何地方,因为GET请求
[HttpGet]
,因为它没有正文(如果OP更改为
[Route]
那么值得一提,可能需要)。多谢各位@freedomn mYou应该得到一个错误(404或405)-始终在ajax请求中添加
错误:
回调以查看发生了什么(或查看网络选项卡)。如果OP希望控制器方法为
HttpGet
??@RyanWilson,那么他们不应该使用方法POST-这是一个或另一个的更改,您的注释(I+1'd)建议更改方法,建议更改属性。此外,该方法是“发送电子邮件”,因此无论如何也不应该是GET(IMO),因为它不是“获取”数据,它正在进行更改。谢谢,完全没有看到这一点,正在查看控制器,从未查看我的ajax查询。所以我只是将我的ajax查询改为get。@freedomn-m他正在获取一些东西,一个关于电子邮件是否发送的状态。@RyanWilson我假设“…”是实际发送电子邮件的代码。POST/PUT仍然允许响应。
$.ajax({
            url: "/SendEmails/sendStudentMailIR/" + studentID + "/" + email + "/" + surID,
            //data: JSON.stringify(ko.toJSON(this)), no need to set data as they are passed in the url
            type: 'GET', //You have a controller method of Get not Post
            dataType: 'json',
            contentType: 'application/json',
            cache: false, //don't cache this request
            success: function (data) {
                alert("Send");
            },
            error: function(jqXHR, exception){ //Added error for exception handling

            }
        });