(精华)2020年6月29日 C#类库 BaseActionFilterAsync(Filter基类)

using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Threading.Tasks;

namespace Core.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);
            }
        }

        /// 
        /// 返回JSON
        /// 
        /// json字符串
        /// 
        public ContentResult JsonContent(string json)
        {
            return new ContentResult { Content = json, StatusCode = 200, ContentType = "application/json; charset=utf-8" };
        }

        /// 
        /// 返回成功
        /// 
        /// 
        public ContentResult Success()
        {
            AjaxResult res = new AjaxResult
            {
                Success = true,
                Msg = "请求成功!"
            };

            return JsonContent(res.ToJson());
        }

        /// 
        /// 返回成功
        /// 
        /// 消息
        /// 
        public ContentResult Success(string msg)
        {
            AjaxResult res = new AjaxResult
            {
                Success = true,
                Msg = msg
            };

            return JsonContent(res.ToJson());
        }

        /// 
        /// 返回成功
        /// 
        /// 返回的数据
        /// 
        public ContentResult Success<T>(T data)
        {
            AjaxResult<T> res = new AjaxResult<T>
            {
                Success = true,
                Msg = "请求成功!",
                Data = data
            };

            return JsonContent(res.ToJson());
        }

        /// 
        /// 返回错误
        /// 
        /// 
        public ContentResult Error()
        {
            AjaxResult res = new AjaxResult
            {
                Success = false,
                Msg = "请求失败!"
            };

            return JsonContent(res.ToJson());
        }

        /// 
        /// 返回错误
        /// 
        /// 错误提示
        /// 
        public ContentResult Error(string msg)
        {
            AjaxResult res = new AjaxResult
            {
                Success = false,
                Msg = msg,
            };

            return JsonContent(res.ToJson());
        }

        /// 
        /// 返回错误
        /// 
        /// 错误提示
        /// 错误代码
        /// 
        public ContentResult Error(string msg, int errorCode)
        {
            AjaxResult res = new AjaxResult
            {
                Success = false,
                Msg = msg,
                ErrorCode = errorCode
            };

            return JsonContent(res.ToJson());
        }
    }
}

你可能感兴趣的:(#,C#类库/Filter,c#,asp.net,后端)