asp.net core读取request内容

在Startup.cs中定义Middleware,设置缓存Http请求的Body数据。代码如下。自定义Middleware请放到Configure方法的最前面。

app.Use(next => new RequestDelegate(
          async context => {
              context.Request.EnableBuffering();
              await next(context);
          }
      )); 

GET请求

HttpContext.Request.Query
//或者
HttpContext.Request.QueryString

POST请求

form

HttpContext.Request.Form

body

HttpContext.Request.Body

//过滤器中使用
public override async Task OnExceptionAsync(ExceptionContext context){
        var httpContext = context.HttpContext;
         var request = httpContext.Request;
         request.Body.Position = 0;
         StreamReader sr = new StreamReader(request.Body);
         string body = await sr.ReadToEndAsync();
         request.Body.Position = 0;
     }
 }

file

HttpContext.Request.Form.Files

你可能感兴趣的:(dotnet,asp.net,后端)