mirror of
https://github.com/dotnetcore/Util.git
synced 2026-10-20 06:07:17 +08:00
重构多次读取Request.Body实现
This commit is contained in:
+1
-1
@@ -3,7 +3,7 @@
|
||||
<VersionMajor>1</VersionMajor>
|
||||
<VersionMinor>0</VersionMinor>
|
||||
<VersionPatch>11</VersionPatch>
|
||||
<VersionQuality>20181122-1</VersionQuality>
|
||||
<VersionQuality>20181123-1</VersionQuality>
|
||||
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
|
||||
<VersionSuffix>preview-$(VersionQuality)</VersionSuffix>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http.Internal;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Util.Helpers;
|
||||
@@ -27,17 +28,33 @@ namespace Util.Webs.Filters {
|
||||
/// </summary>
|
||||
private ILog Logger { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 执行
|
||||
/// </summary>
|
||||
public override async Task OnActionExecutionAsync( ActionExecutingContext context, ActionExecutionDelegate next ) {
|
||||
if( context == null )
|
||||
throw new ArgumentNullException( nameof( context ) );
|
||||
if( next == null )
|
||||
throw new ArgumentNullException( nameof( next ) );
|
||||
ActionFilterAttribute actionFilterAttribute = this;
|
||||
actionFilterAttribute.OnActionExecuting( context );
|
||||
await OnActionExecutingAsync( context );
|
||||
if( context.Result != null )
|
||||
return;
|
||||
var executedContext = await next();
|
||||
actionFilterAttribute.OnActionExecuted( executedContext );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行前
|
||||
/// </summary>
|
||||
public override void OnActionExecuting( ActionExecutingContext context ) {
|
||||
base.OnActionExecuting( context );
|
||||
protected async Task OnActionExecutingAsync( ActionExecutingContext context ) {
|
||||
if( Ignore )
|
||||
return;
|
||||
Logger = GetLog();
|
||||
if( Logger.IsTraceEnabled == false )
|
||||
return;
|
||||
WriteLog( context );
|
||||
await WriteLog( context );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -55,34 +72,34 @@ namespace Util.Webs.Filters {
|
||||
/// <summary>
|
||||
/// 执行前日志
|
||||
/// </summary>
|
||||
private void WriteLog( ActionExecutingContext context ) {
|
||||
private async Task WriteLog( ActionExecutingContext context ) {
|
||||
Logger.Caption( "WebApi跟踪-准备执行操作" )
|
||||
.Class( context.Controller.SafeString() )
|
||||
.Method( context.ActionDescriptor.DisplayName );
|
||||
AddRequestInfo( context );
|
||||
await AddRequestInfo( context );
|
||||
Logger.Trace();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加请求信息参数
|
||||
/// </summary>
|
||||
private void AddRequestInfo( ActionExecutingContext context ) {
|
||||
private async Task AddRequestInfo( ActionExecutingContext context ) {
|
||||
var request = context.HttpContext.Request;
|
||||
Logger.Params( "Http请求方式", request.Method );
|
||||
if( string.IsNullOrWhiteSpace( request.ContentType ) == false )
|
||||
Logger.Params( "ContentType", request.ContentType );
|
||||
AddFormParams( request );
|
||||
await AddFormParams( request );
|
||||
AddCookie( request );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加表单参数
|
||||
/// </summary>
|
||||
private void AddFormParams( Microsoft.AspNetCore.Http.HttpRequest request ) {
|
||||
private async Task AddFormParams( Microsoft.AspNetCore.Http.HttpRequest request ) {
|
||||
if( IsMultipart( request.ContentType ) )
|
||||
return;
|
||||
request.EnableRewind();
|
||||
var result = File.CopyToString( request.Body );
|
||||
var result = await File.ToStringAsync( request.Body, isCloseStream: false );
|
||||
if( string.IsNullOrWhiteSpace( result ) )
|
||||
return;
|
||||
Logger.Params( "表单参数:" ).Params( result );
|
||||
@@ -110,8 +127,7 @@ namespace Util.Webs.Filters {
|
||||
/// <summary>
|
||||
/// 执行后
|
||||
/// </summary>
|
||||
public override void OnResultExecuted( ResultExecutedContext context ) {
|
||||
base.OnResultExecuted( context );
|
||||
public override void OnActionExecuted( ActionExecutedContext context ) {
|
||||
if( Ignore )
|
||||
return;
|
||||
if( Logger.IsTraceEnabled == false )
|
||||
@@ -122,7 +138,7 @@ namespace Util.Webs.Filters {
|
||||
/// <summary>
|
||||
/// 执行后日志
|
||||
/// </summary>
|
||||
private void WriteLog( ResultExecutedContext context ) {
|
||||
private void WriteLog( ActionExecutedContext context ) {
|
||||
Logger.Caption( "WebApi跟踪-执行操作完成" )
|
||||
.Class( context.Controller.SafeString() )
|
||||
.Method( context.ActionDescriptor.DisplayName );
|
||||
@@ -134,7 +150,7 @@ namespace Util.Webs.Filters {
|
||||
/// <summary>
|
||||
/// 添加响应信息参数
|
||||
/// </summary>
|
||||
private void AddResponseInfo( ResultExecutedContext context ) {
|
||||
private void AddResponseInfo( ActionExecutedContext context ) {
|
||||
var response = context.HttpContext.Response;
|
||||
if( string.IsNullOrWhiteSpace( response.ContentType ) == false )
|
||||
Logger.Content( $"ContentType: {response.ContentType}" );
|
||||
@@ -144,7 +160,7 @@ namespace Util.Webs.Filters {
|
||||
/// <summary>
|
||||
/// 记录响应结果
|
||||
/// </summary>
|
||||
private void AddResult( ResultExecutedContext context ) {
|
||||
private void AddResult( ActionExecutedContext context ) {
|
||||
if( !( context.Result is Result result ) )
|
||||
return;
|
||||
Logger.Content( $"响应消息: { result.Message}" )
|
||||
|
||||
+37
-16
@@ -1,5 +1,6 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Util.Helpers {
|
||||
/// <summary>
|
||||
@@ -10,8 +11,24 @@ namespace Util.Helpers {
|
||||
/// 流转换成字符串
|
||||
/// </summary>
|
||||
/// <param name="stream">流</param>
|
||||
public static string ToString( Stream stream ) {
|
||||
return ToString( stream, Encoding.UTF8 );
|
||||
/// <param name="encoding">字符编码</param>
|
||||
/// <param name="bufferSize">缓冲区大小</param>
|
||||
/// <param name="isCloseStream">读取完成是否释放流,默认为true</param>
|
||||
public static string ToString( Stream stream, Encoding encoding = null, int bufferSize = 1024 * 2, bool isCloseStream = true ) {
|
||||
if( stream == null )
|
||||
return string.Empty;
|
||||
if( encoding == null )
|
||||
encoding = Encoding.UTF8;
|
||||
if( stream.CanRead == false )
|
||||
return string.Empty;
|
||||
using( var reader = new StreamReader( stream, encoding, true, bufferSize, !isCloseStream ) ) {
|
||||
if( stream.CanSeek )
|
||||
stream.Seek( 0, SeekOrigin.Begin );
|
||||
var result = reader.ReadToEnd();
|
||||
if( stream.CanSeek )
|
||||
stream.Seek( 0, SeekOrigin.Begin );
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -19,44 +36,48 @@ namespace Util.Helpers {
|
||||
/// </summary>
|
||||
/// <param name="stream">流</param>
|
||||
/// <param name="encoding">字符编码</param>
|
||||
public static string ToString( Stream stream, Encoding encoding ) {
|
||||
/// <param name="bufferSize">缓冲区大小</param>
|
||||
/// <param name="isCloseStream">读取完成是否释放流,默认为true</param>
|
||||
public static async Task<string> ToStringAsync( Stream stream, Encoding encoding = null,int bufferSize = 1024 * 2, bool isCloseStream = true ) {
|
||||
if( stream == null )
|
||||
return string.Empty;
|
||||
if( encoding == null )
|
||||
encoding = Encoding.UTF8;
|
||||
if( stream.CanRead == false )
|
||||
return string.Empty;
|
||||
using( var reader = new StreamReader( stream, encoding ) ) {
|
||||
using( var reader = new StreamReader( stream, encoding, true, bufferSize, !isCloseStream ) ) {
|
||||
if( stream.CanSeek )
|
||||
stream.Seek( 0, SeekOrigin.Begin );
|
||||
return reader.ReadToEnd();
|
||||
var result = await reader.ReadToEndAsync();
|
||||
if( stream.CanSeek )
|
||||
stream.Seek( 0, SeekOrigin.Begin );
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 复制流并转换成字符串
|
||||
/// </summary>
|
||||
/// <param name="stream">流</param>
|
||||
public static string CopyToString( Stream stream ) {
|
||||
return CopyToString( stream, Encoding.UTF8 );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 复制流并转换成字符串
|
||||
/// </summary>
|
||||
/// <param name="stream">流</param>
|
||||
/// <param name="encoding">字符编码</param>
|
||||
public static string CopyToString( Stream stream, Encoding encoding ) {
|
||||
public static async Task<string> CopyToStringAsync( Stream stream, Encoding encoding = null ) {
|
||||
if( stream == null )
|
||||
return string.Empty;
|
||||
if( encoding == null )
|
||||
encoding = Encoding.UTF8;
|
||||
if( stream.CanRead == false )
|
||||
return string.Empty;
|
||||
using ( var memoryStream = new MemoryStream() ) {
|
||||
using( var memoryStream = new MemoryStream() ) {
|
||||
using( var reader = new StreamReader( memoryStream, encoding ) ) {
|
||||
if( stream.CanSeek )
|
||||
stream.Seek( 0, SeekOrigin.Begin );
|
||||
stream.CopyTo( memoryStream );
|
||||
if( memoryStream.CanSeek )
|
||||
memoryStream.Seek( 0, SeekOrigin.Begin );
|
||||
return reader.ReadToEnd();
|
||||
var result = await reader.ReadToEndAsync();
|
||||
if( stream.CanSeek )
|
||||
stream.Seek( 0, SeekOrigin.Begin );
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+15
-1
@@ -1,9 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@@ -102,12 +104,24 @@ namespace Util.Helpers {
|
||||
public static string Body {
|
||||
get {
|
||||
Request.EnableRewind();
|
||||
return File.CopyToString( Request.Body );
|
||||
return File.ToString( Request.Body, isCloseStream: false );
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetBodyAsync(获取请求正文)
|
||||
|
||||
/// <summary>
|
||||
/// 获取请求正文
|
||||
/// </summary>
|
||||
public static async Task<string> GetBodyAsync() {
|
||||
Request.EnableRewind();
|
||||
return await File.ToStringAsync( Request.Body, isCloseStream: false );
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Client( Web客户端 )
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user