.net core在服務(wù)器端獲取api傳遞的參數(shù)過程
這篇文章主要介紹了.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)文章
Asp.NET Core 限流控制(AspNetCoreRateLimit)的實現(xiàn)
這篇文章主要介紹了Asp.NET Core 限流控制(AspNetCoreRateLimit)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
ASP.NET Core 1.0 部署 HTTPS(.NET Core 1.0)
這篇文章主要為大家詳細(xì)介紹了ASP.NET Core 1.0 部署 HTTPS(.NET Core 1.0),感興趣的小伙伴們可以參考一下2016-07-07
詳解在ASP.NET Core中使用Angular2以及與Angular2的Token base身份認(rèn)證
這篇文章主要介紹了詳解在ASP.NET Core中使用Angular2以及與Angular2的Token base身份認(rèn)證,有興趣的可以了解一下。2016-12-12
將Excel中數(shù)據(jù)導(dǎo)入到Access數(shù)據(jù)庫中的方法
將Excel中數(shù)據(jù)導(dǎo)入到Access數(shù)據(jù)庫中的方法,需要的朋友可以參考一下2013-03-03

