欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.net core在服務(wù)器端獲取api傳遞的參數(shù)過程

 更新時間:2019年10月24日 11:09:20   作者:懶東  
這篇文章主要介紹了.net core在服務(wù)器端獲取api傳遞的參數(shù)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了.net core在服務(wù)器端獲取api傳遞的參數(shù)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

在 ActionFilterAttribute 的OnActionExecutionAsync 中使用如下代碼從流中讀取用戶參數(shù)

//從文件流中讀取傳遞測參數(shù)
      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的比較難找,找了將近兩個小時才找到從流中讀取參數(shù)的方法,關(guān)鍵是這句:context.HttpContext.Request.Body.Seek(0, 0);不然讀取的內(nèi)容為空

完整代碼

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)
    {
      //從文件流中讀取傳遞測參數(shù)
      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
 
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論