Infrastructure.UnifiedOutput 1.1.4

dotnet add package Infrastructure.UnifiedOutput --version 1.1.4                
NuGet\Install-Package Infrastructure.UnifiedOutput -Version 1.1.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Infrastructure.UnifiedOutput" Version="1.1.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Infrastructure.UnifiedOutput --version 1.1.4                
#r "nuget: Infrastructure.UnifiedOutput, 1.1.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Infrastructure.UnifiedOutput as a Cake Addin
#addin nuget:?package=Infrastructure.UnifiedOutput&version=1.1.4

// Install Infrastructure.UnifiedOutput as a Cake Tool
#tool nuget:?package=Infrastructure.UnifiedOutput&version=1.1.4                

Infrastructure.UnifiedOutput

webapi 统一输出结果

输出结构定义

/// <summary>
/// 参照企业微信Api,任意接口输出的响应应继承此类
/// </summary>
public class ResultDto
{
    /// <summary>
    /// 实例化时默认就是成功的
    /// </summary>
    public int errcode { get; set; } = ResultConsts.DEFAULT_SUCCESS_CODE; // 0为成功(无错误)

    public string errmsg { get; set; }

    /// <summary>
    /// 服务端处理需要,不需要响应输出
    /// </summary>
    [JsonIgnore]
    public bool issuccess { get { return this.errcode == ResultConsts.DEFAULT_SUCCESS_CODE; } }
}
/// <summary>
/// 泛型的响应输出
/// </summary>
/// <typeparam name="T"></typeparam>
public class ResultDto<T>
{
    /// <summary>
    /// 实例化时默认就是成功的
    /// </summary>
    public int errcode { get; set; } = ResultConsts.DEFAULT_SUCCESS_CODE; // 0为成功(无错误)

    public string errmsg { get; set; }

    /// <summary>
    /// 服务端处理需要,不需要响应输出
    /// </summary>
    [JsonIgnore]
    public bool issuccess { get { return this.errcode == ResultConsts.DEFAULT_SUCCESS_CODE; } }

    /// <summary>
    /// 返回数据包到 data 中
    /// </summary>
    public T data { get; set; }
}

常用的结果处理,可以根据情况进行对应引用


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 

namespace Infrastructure.UnifiedOutput
{

    public static class Result
    {

        /// <summary>
        /// 一个有消息提示有数据的成功响应
        /// </summary>
        /// <typeparam name="TValue">响应结果继承ResultDto</typeparam>
        /// <param name="result">响应结果</param>
        /// <param name="message">响应结果要提示的消息</param>
        /// <returns></returns>
        public static TValue Ok<TValue>(TValue result, string message = "") where TValue : ResultDto
        {
            result.errmsg = message;
            return result;
        }
         
        /// <summary>
        /// 一个没有消息提示没有附带数据的成功响应
        /// </summary>
        /// <returns></returns>
        public static ResultDto Ok()
        {
            return new ResultDto();
        }

        /// <summary>
        /// 一个有消息提示没有数据的成功响应
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ResultDto Ok(string message)
        {
            return new ResultDto() { errmsg = message };
        }

        /// <summary>
        /// 一个有消息提示有数据的失败响应
        /// </summary>
        /// <typeparam name="TValue">响应结果继承ResultDto</typeparam>
        /// <param name="message">响应结果要提示的消息</param>
        /// <param name="errorCode">错误代码,默认是 10 </param>
        /// <returns></returns>
        public static TValue Fail<TValue>(string message, int errorCode = ResultConsts.DEFAULT_FAIL_CODE) where TValue : ResultDto, new()
        {
            var result = new TValue();// default(TValue);
            result.errcode = errorCode;
            result.errmsg = message;
            return result;
        }

        /// <summary>
        /// 一个有消息提示没有数据的失败响应
        /// </summary>
        /// <param name="message">响应结果要提示的消息</param>
        /// <param name="errorCode">错误代码,默认是 10 </param>
        /// <returns></returns>
        public static ResultDto Fail(string message, int errorCode = ResultConsts.DEFAULT_FAIL_CODE)
        {
            return new ResultDto
            {
                errcode = errorCode,
                errmsg = message
            };
        }

        /// <summary>
        /// 一个提示内容是程序异常的失败响应
        /// </summary>
        /// <param name="ex">捕获的异常</param>
        /// <param name="errorCode">错误代码,默认是 10</param>
        /// <returns></returns>
        public static ResultDto Fail(Exception ex, int errorCode = ResultConsts.DEFAULT_FAIL_CODE)
        {
            return new ResultDto
            {
                errcode = errorCode,
                errmsg = ex.Message
            };
        }

        /// <summary>
        /// 一个根据bool判断是成功或失败的响应,响应包含提示,没有数据
        /// </summary>
        /// <param name="issuccess"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ResultDto IsOk(bool issuccess, string message)
        {
            return new ResultDto
            {
                errcode = issuccess.ToErrorCode(),
                errmsg = message
            };
        }

        /// <summary>
        /// 一个根据bool判断是成功或失败的响应,响应包含提示和数据
        /// </summary>
        /// <typeparam name="TValue">响应结果继承ResultDto</typeparam>
        /// <param name="result">响应结果</param>
        /// <param name="message">响应结果要提示的消息</param>
        /// <returns></returns>
        public static TValue IsOk<TValue>(TValue result, bool issuccess, string message) where TValue : ResultDto
        {
            result.errcode = issuccess.ToErrorCode();
            result.errmsg = message;
            return result;
        }

        /// <summary>
        ///  一个根据自身是否为空的转换响应
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public static T IsOk<T>(T dto, string errorMessage = "记录不存在") where T : ResultDto, new()
        {
            if (dto == null)
            {
                return Result.Fail<T>(errorMessage);
            }
            else
            {
                return dto;
            }
        }

        /// <summary>
        /// 默认的成功/失败码
        /// </summary>
        /// <param name="issuccess"></param>
        /// <returns></returns>
        public static int ToErrorCode(this bool issuccess)
        {
            return issuccess ? ResultConsts.DEFAULT_SUCCESS_CODE : ResultConsts.DEFAULT_FAIL_CODE;
        }

        /// <summary>
        /// 默认的成功/失败码
        /// </summary>
        /// <param name="issuccess"></param>
        /// <returns></returns>
        public static int ErrorCode(bool issuccess)
        {
            return issuccess.ToErrorCode();
        }

        /// <summary>
        /// 获取一个分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static PageResultDto<T> Page<T>(List<T> items, int total)  
        {
            return new PageResultDto<T>
            {
                TotalCount = total,
                Items = items
            };
        }

        /// <summary>
        /// 获取一个分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static PageResultDto<T> Page<T>(List<T> items, long total)
        {
            return Page(items, (int)total);
        }
    }
}

分页进行了统一定义

/// <summary>
/// 分页的返回
/// </summary>
public class PageResultDto<T> : ResultDto 
{
    /// <summary>
    /// 记录总数
    /// </summary>
    public int TotalCount { get; set; } 

    public List<T> Items { get; set; } = new List<T>();
}
/// <summary>
/// 获取一个分页
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <param name="total"></param>
/// <returns></returns>
public static PageResultDto<T> Page<T>(List<T> items, int total)  
{
    return new PageResultDto<T>
    {
        TotalCount = total,
        Items = items
    };
}

/// <summary>
/// 获取一个分页
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <param name="total"></param>
/// <returns></returns>
public static PageResultDto<T> Page<T>(List<T> items, long total)
{
    return Page(items, (int)total);
}

 #region 泛型范畴
/// <summary>
/// 指定响应的错误代码、消息、数据等
/// </summary>
/// <param name="errcode">错误代码,0 为无错误</param>
/// <param name="errmsg">消息</param>
/// <param name="data">数据</param>
public static ResultDto<T> New<T>(int errcode, string errmsg, T data = default(T))
{
    return new ResultDto<T>
    {
        errcode = errcode,
        errmsg = errmsg,
        data = data
    };
}

/// <summary>
/// 无错误的包含代码(errcode=0)、消息、数据的响应
/// </summary>
/// <param name="errmsg">消息,有时并不需要消息,就要逻辑数据,故默认= ""</param>
/// <param name="data">数据</param>
public static ResultDto<T> New<T>(string errmsg, T data = default(T))
{
    return new ResultDto<T>
    {
        errmsg = errmsg,
        data = data
    };
}

/// <summary>
/// 无错误的包含代码(errcode=0)、消息、数据的响应
/// </summary> 
/// <param name="data">数据</param>
public static ResultDto<T> New<T>(T data = default(T))
{
    return new ResultDto<T>
    {
        data = data
    };
}  

/// <summary>
/// 结果true or false; true errorCode = 0, false errorCode = CommonError(10001)
/// </summary>
/// <param name="success">错误代码,0 为无错误</param>
/// <param name="errmsg">消息</param>
/// <param name="data">数据</param>
public static ResultDto<T> New<T>(bool success, string errmsg, T data = default(T))
{
    return new ResultDto<T>
    {
        errcode = ToErrorCode( success)  ,
        errmsg = errmsg,
        data = data
    };
}   

#endregion
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Infrastructure.UnifiedOutput:

Package Downloads
Infrastructure.AccessToken.Authorization

Package Description

Infrastructure.AccessToken.Domain

Package Description

Infrastructure.SharedAppsettings.Client

Package Description

Infrastructure.Utils.EncryptFilter

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.4 752 8/25/2022
1.1.3 384 8/24/2022
1.1.2 425 8/17/2022
1.1.1 380 8/16/2022
1.1.0 472 4/3/2022
1.0.4 444 3/19/2022
1.0.3 422 3/18/2022
1.0.2 430 3/18/2022
1.0.1 411 3/17/2022
1.0.0 419 3/16/2022