Net5.0里面通过HttpContext获取post、get请求参数信息

项目场景:

net5.0中使用httpcontext获取里面传输的post,get参数


直接上代码:

//获取post参数 思路:以流的形式读取body里面的参数

 public async Task Invoke(HttpContext context)
 {
    
    
  	HttpRequest request = context.Request;
    Stream straem = request.Body;
    Encoding encoding = Encoding.UTF8;
    string date = string.Empty;
    using (StreamReader reader = new StreamReader(straem, encoding))
    {
    
    
    	//date就是post传过来的值json格式
       date = reader.ReadToEndAsync().Result;
    }
 }

//获取get参数

 context.Request.QueryString.Value

这句话就可以获取到了,读取到的是一个字符串 例:
?userid=‘123’&username=‘张三’
就是这样一个样式

猜你喜欢

转载自blog.csdn.net/qq_37213281/article/details/122476556