.net core 获取http请求参数


POST请求

1)通过Request.Body原始流获取Body信息

控制器接收代码:

        #region 根据body流读取body信息
        [HttpPost]
        public async Task<IActionResult> GetRequestInfoByBody()
        {
            this.HttpContext.Request.EnableBuffering(); // 允许多次读取请求体
            var stream = Request.Body;
            if (stream != null)
            {
                using (var reader = new StreamReader(stream, Encoding.UTF8))
                {
                    string body =await reader.ReadToEndAsync();
                    var bodyModel = Newtonsoft.Json.JsonConvert.DeserializeObject<OneBodyModel>(body);
                }
            }
            return Json(new { isSuccess=true });
        }
        #endregion

客户端发送Ajax请求示例:

        var xhr = new XMLHttpRequest();
        xhr.open('post', '/Home/GetRequestInfoByBody');
        xhr.responseType = 'json';
        xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
        xhr.send(JSON.stringify({ code: 1, msg: 'Test', data: [1, 2, 3] }));
        xhr.onloadend = function () {
            if (this.status == 200) {
                alert('ok');
            }
        }

测试:

 

参考链接

https://www.cnblogs.com/kklldog/p/core-mvc-modelbind.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM