.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