基于不同形式的json响应处理

Fork Me On Github
zodream 其他技术 C# 2020年09月

我的json格式

普通json响应

这是普通链接响应json格式,不论是否出错,响应状态码都是 200

json
        
{
    "code": 200, // 200 表示程序处理成功,其他数字自定义
    "status": "success", // success 表示成功 failure 表示失败
    "message": "",      // 成功的消息提示
    "errors": "",     // 失败的信息
    "data": {},       // 成功响应的信息
    "url": "",         // 搭配 code: 302 实现页面跳转
}
12345678

具体实例

成功返回用户信息

json
        
{
    "code": 200,
    "status": "success",
    "data": {
        "id": 1,
        "name": "zodream"
    },
}
12345678

失败返回提示无权限需要登录

json
      
{
    "code": 302,
    "status": "failure",
    "errors": "当前账户无权限,请先登录",
    "url": "/login?redirect_uri=/"
}
123456

RESTful 响应

状态码判断是否处理成功

具体实例

成功返回用户信息

HTTP/1.1 200 OK

json
    
{
    "id": 1,
    "name": "zodream"
}
1234

成功返回用户列表

HTTP/1.1 200 OK

json
        
{
    "data": [
        {
            "id": 1,
            "name": "zodream"
        }
    ]
}
12345678

失败返回提示无权限需要登录

HTTP/1.1 302 Found

json
    
{
    "code": 302,
    "message": "当前账户无权限,请先登录",
}
1234

具体代码实现,

netcore 版本为例

c#
              
    public interface IJsonResponse
    {
        public object Render(object data);

        public object RenderData(object data);

        public object RenderData(object data, string message);

        public object RenderPage<T>(Page<T> page);

        public object RenderFailure(string message, int code);

        public object RenderFailure(string message);
    }
1234567891011121314

普通版本的实现

c#
                                                             
    public class JsonResponse : IJsonResponse
    {
        public object Render(object data)
        {
            return data;
        }

        public object RenderData(object data)
        {
            return Render(new
            {
                code = 200,
                status = "success",
                data
            });
        }

        public object RenderData(object data, string message)
        {
            return Render(new
            {
                code = 200,
                status = "success",
                data,
                message
            });
        }

        public object RenderFailure(string message, int code)
        {
            return new
            {
                code,
                status = "failure",
                message
            };
        }

        public object RenderFailure(string message)
        {
            return RenderFailure(message, 404);
        }

        public object RenderPage<T>(Page<T> page)
        {

            return Render(new
            {
                code = 200,
                status = "success",
                data = page.Items,
                paging = new
                {
                    limit = page.ItemsPerPage,
                    offset = page.CurrentPage,
                    total = page.TotalItems,
                    more = page.CurrentPage < page.TotalPages
                }
            });
        }
    }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061

RESTful 的实现

c#
                                                          
    public class PlatformResponse : IJsonResponse
    {

        public PlatformModel Platform { get; set; }

        public object Render(object data)
        {
            return data;
        }

        public object RenderData(object data)
        {
            return Render(new
            {
                data,
                appid = Platform.Appid
            });
        }

        public object RenderData(object data, string message)
        {
            return Render(new
            {
                data,
                message
            });
        }

        public object RenderFailure(string message, int code)
        {
            return new
            {
                code,
                message
            };
        }

        public object RenderFailure(string message)
        {
            return RenderFailure(message, 404);
        }

        public object RenderPage<T>(Page<T> page)
        {

            return Render(new
            {
                data = page.Items,
                paging = new
                {
                    limit = page.ItemsPerPage,
                    offset = page.CurrentPage,
                    total = page.TotalItems,
                    more = page.CurrentPage < page.TotalPages
                }
            });
        }
    }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758

通过中间件注入当前请求

c#
     
public Task InvokeAsync(HttpContext context)
{
    context.Items.Add("json", new JsonResponse());
    return _next.Invoke(context);
}
12345

再控制器中使用

c#
 
HttpContext.Items["json"] as IJsonResponse
1

在这里有一个问题,即状态码并没有实现,可以考虑拓展方法,把HttpContext当作参数传入,内部做状态码输入

点击查看全文
0 251 0