.net core在服务器端获取api传递的参数过程

所属分类: 网络编程 / ASP.NET 阅读数: 270
收藏 0 赞 0 分享

这篇文章主要介绍了.net core在服务器端获取api传递的参数过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

在 ActionFilterAttribute 的OnActionExecutionAsync 中使用如下代码从流中读取用户参数

//从文件流中读取传递测参数
      using (var ms = new MemoryStream())
      {
        context.HttpContext.Request.Body.Seek(0, 0);//将读取指针迻到开始位置
        context.HttpContext.Request.Body.CopyTo(ms);
        var b = ms.ToArray();
        var postParamsString = Encoding.UTF8.GetString(b);
      }

虽然以前就知道是从流中读取,但是.net core的比较难找,找了将近两个小时才找到从流中读取参数的方法,关键是这句:context.HttpContext.Request.Body.Seek(0, 0);不然读取的内容为空

完整代码

public class SignValidateAttribute : ActionFilterAttribute
  {
    #region
    /// <summary>
    ///
    /// </summary>
    /// <param name="context"></param>
    /// <param name="next"></param>
    /// <returns></returns>
    public async override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
      //从文件流中读取传递测参数
      using (var ms = new MemoryStream())
      {
        context.HttpContext.Request.Body.Seek(0, 0);
        context.HttpContext.Request.Body.CopyTo(ms);
        var b = ms.ToArray();
        var postParamsString = Encoding.UTF8.GetString(b);
        await next();
      }
    }
 
    /// <summary>
    ///
    /// </summary>
    /// <param name="context"></param>
    /// <param name="next"></param>
    /// <returns></returns>
    public override Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
    {
      //string dataJson = GetContextJson(context.);
      return base.OnResultExecutionAsync(context, next);
    }
    #endregion
 
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Asp.net中的mail的发送

Asp.net中的mail的发送
收藏 0 赞 0 分享

用ASP.Net实现文件的在线压缩和解压缩

用ASP.Net实现文件的在线压缩和解压缩
收藏 0 赞 0 分享

ASP.NET中文件上传下载方法集合

ASP.NET中文件上传下载方法集合
收藏 0 赞 0 分享

ASP.NET通过Remoting service上传文件

ASP.NET通过Remoting service上传文件
收藏 0 赞 0 分享

ASP.NET2.0服务器控件之Render方法

ASP.NET2.0服务器控件之Render方法
收藏 0 赞 0 分享

ASP.NET2.0 WebRource,开发微调按钮控件

ASP.NET2.0 WebRource,开发微调按钮控件
收藏 0 赞 0 分享

ASP.NET2.0新特性概述

ASP.NET2.0新特性概述
收藏 0 赞 0 分享

介绍几个ASP.NET中容易忽略但却很重要的方法函数

介绍几个ASP.NET中容易忽略但却很重要的方法函数
收藏 0 赞 0 分享

asp.net2.0如何加密数据库联接字符串

asp.net2.0如何加密数据库联接字符串
收藏 0 赞 0 分享

用.NET 2.0压缩/解压功能处理大型数据

用.NET 2.0压缩/解压功能处理大型数据
收藏 0 赞 0 分享
查看更多