getting the request body inside HttpContext from a Middleware in asp.net core 2.0

前端 未结 4 1748
礼貌的吻别
礼貌的吻别 2021-02-20 10:45

I am having a simple middleware which fetches the body of the request and store it in a string. It is reading fine the stream, but the issue is it wont call my controller which

4条回答
  •  忘了有多久
    2021-02-20 11:04

    using (var mem = new MemoryStream())
                using (var reader = new StreamReader(mem))
                {
                    Request.Body.CopyTo(mem);
                    var body = reader.ReadToEnd();
    
    //and this you can reset the position of the stream.
    
                    mem.Seek(0, SeekOrigin.Begin);
                    body = reader.ReadToEnd();
                }
    

    Here you are can read how it works. https://gunnarpeipman.com/aspnet-core-request-body/

提交回复
热议问题