1. 程式人生 > 實用技巧 >當 method =post時,讀取inputStream到RequestBody

當 method =post時,讀取inputStream到RequestBody

        /// <summary>
        /// 當 method =post時,讀取inputStream到RequestBody
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!this.ModelState.IsValid) {
                filterContext.Result 
= Json(new { message = ModelStateHelper.GetModelErrorMessages(ModelState), code = ResponseCode.錯誤請求 }); return; } if (Request.HttpMethod.Equals("POST")) { var pos = Request.InputStream.Position; Request.InputStream.Position
= 0; var ln = Request.InputStream.Length; using (var sr = new StreamReader(Request.InputStream)) { this.RequestBody = sr.ReadToEnd(); ln = Request.InputStream.Length; Request.InputStream.Position
= pos; logger.Info("RequestBody:" + RequestBody); } if (!Signature()) { filterContext.Result = Json(new { code = ResponseCode.錯誤請求, message = "認證失敗!", }); } } }