fix 确保null异常和write response问题
This commit is contained in:
@@ -17,21 +17,22 @@ public class IgnoreDataCaseAttribute : Attribute, IAsyncResultFilter
|
|||||||
var type = result.Value?.GetType();
|
var type = result.Value?.GetType();
|
||||||
if (type.IsGenericType && type.IsAssignableTo(typeof(IApiResult)))
|
if (type.IsGenericType && type.IsAssignableTo(typeof(IApiResult)))
|
||||||
{
|
{
|
||||||
var apiResult = result.Value as DefaultApiResult<object>;
|
if (result.Value is DefaultApiResult<object> apiResult && !context.HttpContext.Response.HasStarted)
|
||||||
|
|
||||||
var body = new
|
|
||||||
{
|
{
|
||||||
code = 200,
|
var body = new
|
||||||
msg = "操作成功",
|
{
|
||||||
data = apiResult.Data
|
code = 200,
|
||||||
};
|
msg = "操作成功",
|
||||||
|
data = apiResult.Data
|
||||||
|
};
|
||||||
|
|
||||||
var json = JsonConvert.SerializeObject(body);
|
var json = JsonConvert.SerializeObject(body);
|
||||||
var bytes = Encoding.UTF8.GetBytes(json);
|
var bytes = Encoding.UTF8.GetBytes(json);
|
||||||
|
|
||||||
context.HttpContext.Response.ContentType = "application/json";
|
context.HttpContext.Response.ContentType = "application/json";
|
||||||
context.HttpContext.Response.ContentLength = bytes.Length;
|
context.HttpContext.Response.ContentLength = bytes.Length;
|
||||||
await context.HttpContext.Response.BodyWriter.WriteAsync(bytes);
|
await context.HttpContext.Response.BodyWriter.WriteAsync(bytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user