• 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());
            }
        }
    }
  • 相关阅读:
    记录一次阿里云服务器被攻击的经历
    post字符 特殊字符处理【转】
    forkjoin框架疑问记录
    centos7 安装 maven 和ant git 以及 rocketmq 4.2安装过程(安装成功,调用失败)
    IntelliJ Idea编译报错:javacTask: 源发行版 1.8 需要目标发行版 1.8
    记录一次json转换的经历
    maven项目在idea下右键不出现maven的解决办法
    Mac系统查看端口占用和杀死进程
    委托所想
    win8中的参数传递
  • 原文地址:https://www.cnblogs.com/enych/p/13503447.html
Copyright © 2020-2023  润新知