修改问题
This commit is contained in:
parent
add717cbf6
commit
a2d4a58b6b
|
|
@ -32,66 +32,76 @@ public class CustomResultFilter : IResultFilter
|
|||
/// <param name="context"></param>
|
||||
public void OnResultExecuting(ResultExecutingContext context)
|
||||
{
|
||||
var httpContext = context.HttpContext;
|
||||
var path = httpContext.Request.Path.Value ?? "";
|
||||
var apiPrefix = path.Replace('/', '.').TrimStart('.');
|
||||
var sw = Stopwatch.StartNew();
|
||||
CloudGamingBase cloudGamingBase = new CloudGamingBase(_serviceProvider);
|
||||
//// 获取当前用户的信息
|
||||
//var user = httpContext.User.Identity.IsAuthenticated ? httpContext.User.Identity.Name : "Anonymous";
|
||||
if (context.Result is ObjectResult objectResult && objectResult.Value != null)
|
||||
if (context.Result is ObjectResult objectResult)
|
||||
{
|
||||
var x = objectResult.Value.GetType();
|
||||
object? value = null;
|
||||
if (!x.FullName.Contains("HuanMeng.DotNetCore.Base.BaseResponse"))
|
||||
if (objectResult.Value == null)
|
||||
{
|
||||
//特殊处理
|
||||
if (objectResult.Value is string)
|
||||
{
|
||||
BaseResponse<string> baseStringResponse = new BaseResponse<string>(ResponseCode.Success, "", objectResult.Value.ToString());
|
||||
value = baseStringResponse;
|
||||
if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
|
||||
{
|
||||
var messageAttribute = MessageAttributeExtend.GetMessageAttribute(controllerActionDescriptor);
|
||||
// 如果存在MessageAttribute,则设置响应消息
|
||||
if (messageAttribute != null)
|
||||
{
|
||||
baseStringResponse.Message = messageAttribute.Message;
|
||||
}
|
||||
}
|
||||
sw.Stop();
|
||||
context.HttpContext.Response.Headers.TryAdd("X-Request-Duration-Filter", sw.Elapsed.TotalMilliseconds.ToString());
|
||||
objectResult.Value = baseStringResponse.ToString();
|
||||
return;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
BaseResponse<object> baseResponse = new BaseResponse<object>(ResponseCode.Success, "", objectResult.Value);
|
||||
value = baseResponse;
|
||||
// 获取当前执行的Action方法的信息并进行类型检查
|
||||
if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
|
||||
{
|
||||
var messageAttribute = MessageAttributeExtend.GetMessageAttribute(controllerActionDescriptor);
|
||||
// 如果存在MessageAttribute,则设置响应消息
|
||||
if (messageAttribute != null)
|
||||
{
|
||||
baseResponse.Message = messageAttribute.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
var dic = value.ToDictionaryOrList(apiPrefix, it => cloudGamingBase.Cache.ImageEntityCache[it]);
|
||||
objectResult.Value = dic;
|
||||
BaseResponse<string> baseStringResponse = new BaseResponse<string>(ResponseCode.Success, "", null);
|
||||
objectResult.Value = baseStringResponse.ToString();
|
||||
return;
|
||||
}
|
||||
//else
|
||||
//{
|
||||
// //value = objectResult.Value;
|
||||
//}
|
||||
|
||||
// 获取当前用户的信息
|
||||
if (objectResult.Value != null)
|
||||
{
|
||||
var httpContext = context.HttpContext;
|
||||
var path = httpContext.Request.Path.Value ?? "";
|
||||
var apiPrefix = path.Replace('/', '.').TrimStart('.');
|
||||
var sw = Stopwatch.StartNew();
|
||||
CloudGamingBase cloudGamingBase = new CloudGamingBase(_serviceProvider);
|
||||
var x = objectResult.Value.GetType();
|
||||
object? value = null;
|
||||
if (!x.FullName.Contains("HuanMeng.DotNetCore.Base.BaseResponse"))
|
||||
{
|
||||
//特殊处理
|
||||
if (objectResult.Value is string)
|
||||
{
|
||||
BaseResponse<string> baseStringResponse = new BaseResponse<string>(ResponseCode.Success, "", objectResult.Value.ToString());
|
||||
value = baseStringResponse;
|
||||
if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
|
||||
{
|
||||
var messageAttribute = MessageAttributeExtend.GetMessageAttribute(controllerActionDescriptor);
|
||||
// 如果存在MessageAttribute,则设置响应消息
|
||||
if (messageAttribute != null)
|
||||
{
|
||||
baseStringResponse.Message = messageAttribute.Message;
|
||||
}
|
||||
}
|
||||
sw.Stop();
|
||||
context.HttpContext.Response.Headers.TryAdd("X-Request-Duration-Filter", sw.Elapsed.TotalMilliseconds.ToString());
|
||||
objectResult.Value = baseStringResponse.ToString();
|
||||
return;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
BaseResponse<object> baseResponse = new BaseResponse<object>(ResponseCode.Success, "", objectResult.Value);
|
||||
value = baseResponse;
|
||||
// 获取当前执行的Action方法的信息并进行类型检查
|
||||
if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
|
||||
{
|
||||
var messageAttribute = MessageAttributeExtend.GetMessageAttribute(controllerActionDescriptor);
|
||||
// 如果存在MessageAttribute,则设置响应消息
|
||||
if (messageAttribute != null)
|
||||
{
|
||||
baseResponse.Message = messageAttribute.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
var dic = value.ToDictionaryOrList(apiPrefix, it => cloudGamingBase.Cache.ImageEntityCache[it]);
|
||||
objectResult.Value = dic;
|
||||
}
|
||||
//else
|
||||
//{
|
||||
// //value = objectResult.Value;
|
||||
//}
|
||||
|
||||
|
||||
sw.Stop();
|
||||
context.HttpContext.Response.Headers.TryAdd("X-Request-Duration-Filter", sw.Elapsed.TotalMilliseconds.ToString());
|
||||
sw.Stop();
|
||||
context.HttpContext.Response.Headers.TryAdd("X-Request-Duration-Filter", sw.Elapsed.TotalMilliseconds.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user