• net core 方法 返回值 重改?


    using BFManage.Util;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Filters;
    
    namespace BFManage.Api
    {
        /// <summary>
        /// 若Action返回对象为自定义对象,则将其转为JSON
        /// </summary>
        public class FormatResponseAttribute : BaseActionFilter, IActionFilter
        {
            /// <summary>
            /// Action执行之前执行
            /// </summary>
            /// <param name="context">过滤器上下文</param>
            public void OnActionExecuting(ActionExecutingContext context)
            {
    
            }
    
            /// <summary>
            /// Action执行完毕之后执行
            /// </summary>
            /// <param name="context"></param>
            public void OnActionExecuted(ActionExecutedContext context)
            {
                if (context.ContainsFilter<NoJsonParamterAttribute>())
                    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);
                }
            }
        }
    }


    using BFManage.Business;
    using BFManage.Util;
    using Microsoft.AspNetCore.Mvc;
    using System;
    
    namespace BFManage.Api
    {
        public abstract class BaseActionFilter : Attribute
        {
            public IOperator Operator { get => AutofacHelper.GetScopeService<IOperator>(); }
    
            /// <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());
            }
        }
    }
  • 相关阅读:
    循环获取数据
    implode
    获取二维数组中的值
    根据id获取某一类的最大最小值
    array_column的作用
    用curl模拟夹带cookie的http请求
    phpunit——执行测试文件和测试文件中的某一个函数
    call_user_func
    9 [面向对象]-内置方法
    8 [面向对象]-反射
  • 原文地址:https://www.cnblogs.com/enych/p/13503447.html
Copyright © 2020-2023  润新知