ASP.NET Core统一接口返回模型
利用IAsyncActionFilter特性来统一接口返回数据模型
C# 全选
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Threading.Tasks;
namespace Coldairarrow.Api
{
/// <summary>
/// 若Action返回对象为自定义对象,则将其转为JSON
/// </summary>
public class FormatResponseAttribute : BaseActionFilterAsync
{
public override async Task OnActionExecuted(ActionExecutedContext context)
{
if (context.ContainsFilter<NoFormatResponseAttribute>())
return;
if (context.Result is EmptyResult)
context.Result = Success();
else if (context.Result is ObjectResult res)
{
if (res.Value is AjaxResult)
context.Result = JsonContent(res.Value.ToJson());
else
context.Result = Success(res.Value);
}
await Task.CompletedTask;
}
}
}
C# 全选
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Threading.Tasks;
namespace Coldairarrow.Api
{
public abstract class BaseActionFilterAsync : Attribute, IAsyncActionFilter
{
public async virtual Task OnActionExecuting(ActionExecutingContext context)
{
await Task.CompletedTask;
}
public async virtual Task OnActionExecuted(ActionExecutedContext context)
{
await Task.CompletedTask;
}
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
await OnActionExecuting(context);
if (context.Result == null)
{
var nextContext = await next();
await OnActionExecuted(nextContext);
}
}
/// <summary>
/// 返回JSON
/// </summary>
/// <param name="json">json字符串</param>
/// <returns></returns>
public ContentResult JsonContent(string json)
{
return new ContentResult { Content = json, StatusCode = 200, ContentType = "application/json; charset=utf-8" };
}
/// <summary>
/// 返回成功
/// </summary>
/// <returns></returns>
public ContentResult Success()
{
AjaxResult res = new AjaxResult
{
Success = true,
Msg = "请求成功!"
};
return JsonContent(res.ToJson());
}
/// <summary>
/// 返回成功
/// </summary>
/// <param name="msg">消息</param>
/// <returns></returns>
public ContentResult Success(string msg)
{
AjaxResult res = new AjaxResult
{
Success = true,
Msg = msg
};
return JsonContent(res.ToJson());
}
/// <summary>
/// 返回成功
/// </summary>
/// <param name="data">返回的数据</param>
/// <returns></returns>
public ContentResult Success<T>(T data)
{
AjaxResult<T> res = new AjaxResult<T>
{
Success = true,
Msg = "请求成功!",
Data = data
};
return JsonContent(res.ToJson());
}
/// <summary>
/// 返回错误
/// </summary>
/// <returns></returns>
public ContentResult Error()
{
AjaxResult res = new AjaxResult
{
Success = false,
Msg = "请求失败!"
};
return JsonContent(res.ToJson());
}
/// <summary>
/// 返回错误
/// </summary>
/// <param name="msg">错误提示</param>
/// <returns></returns>
public ContentResult Error(string msg)
{
AjaxResult res = new AjaxResult
{
Success = false,
Msg = msg,
};
return JsonContent(res.ToJson());
}
/// <summary>
/// 返回错误
/// </summary>
/// <param name="msg">错误提示</param>
/// <param name="errorCode">错误代码</param>
/// <returns></returns>
public ContentResult Error(string msg, int errorCode)
{
AjaxResult res = new AjaxResult
{
Success = false,
Msg = msg,
ErrorCode = errorCode
};
return JsonContent(res.ToJson());
}
}
}
控制器中使用
C# 全选
/// <summary>
/// 基控制器
/// </summary>
[FormatResponse]
public class BaseController : ControllerBase
{
}
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
post 张国生