惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
O
OpenAI News
S
Secure Thoughts
H
Heimdal Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
S
Security Affairs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
The Register - Security
The Register - Security
GbyAI
GbyAI
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Cloudflare Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Webroot Blog
Webroot Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
T
Tor Project blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
V
V2EX
T
Tailwind CSS Blog
SecWiki News
SecWiki News
NISL@THU
NISL@THU
C
Check Point Blog

博客园 - 强悍的抽屉

基于 Dapper 的一个 DbUtils c#版 mqtt 3.1.1 client 实现 mqtt 协议之 PINGREQ, PINGRESP httpWebRequest 文件下载 一个 go 文件服务器 ssdb MongoDB 刷新几次就报错 C# Win32API - 强悍的抽屉 - 博客园 回车跳转控件焦点 让程序只启动一次 -- Mutex C# 排序 WINDEF.h 变量类型 SqlHelper 数据库操作类2 SqlHelper 数据库操作类 第一个 Windows 应用程序 JavaScript 字符串处理函数 - 强悍的抽屉 - 博客园 JavaScript 字符串函数扩充 - 强悍的抽屉 - 博客园 C# 字符串处理一些方法 希望找人一起写个 Ajax 的封装 几种流行的JS框架的选择
WebAPI 操作返回
强悍的抽屉 · 2016-05-03 · via 博客园 - 强悍的抽屉

定义了一个返回枚举:

    public enum ResultExceptionEnum
    {
        积分不足       = 4002,
        支付失败       = 4003,
        用户不存在     = 4004,
        验证码发送失败 = 4005,
        验证码不正确   = 4006,
        账号已存在     = 4007,
        昵称已存在     = 4008,

        公会不存在     = 4100,
        公会名称已存在 = 4101,
        用户不在此公会 = 4102,

        社区不存在     = 4200,
        社区名称已存在 = 4201,
    }

定义一个返回 Exception

    public class ResultException : Exception
    {
        public ResultException(){}

        public ResultException(int code, string msg)
        {
            Code = code;
            Msg = msg;
        }

        public ResultException(ResultExceptionEnum code)
        {
            Code = code.GetHashCode();
            Msg = Enum.GetName(typeof(ResultExceptionEnum), code);
        }

        public int Code { get; set; }

        public string Msg { get; set; }

        public override string ToString()
        {
            return JsonConvert.SerializeObject(new { code = Code, msg = Msg });
        }

        public object ToResult()
        {
            var obj = new { code = Code, msg = Msg };
            return obj;
        }
    }

删除操作返回:

        // DELETE: api/Values/5
        public IHttpActionResult Delete(int id)
        {

            try
            {
                throw new ResultException(ResultExceptionEnum.验证码不正确);
            }
            catch (ResultException ex)
            {
                return Ok(ex.ToResult());
            }
            //return Ok(new { code = 200, msg = "删除成功" });
        }

上面两个风格,

1.  抛出异常,返回。

2. 直接返回

结果:

再来个异常版:

        // DELETE: api/Values/5
        public IHttpActionResult Delete(int id)
        {
            //try
            //{
            //    throw new ResultException(ResultExceptionEnum.验证码不正确);
            //}
            //catch (ResultException ex)
            //{
            //    return Ok(ex.ToResult());
            //}
            //return Ok(new { code = 200, msg = "删除成功" });

            throw new ResultException(ResultExceptionEnum.验证码不正确);
        }

配置下:

WebApiConfig

config.Filters.Add(new WebApiExceptionFilter());

    /// <summary>
    /// 全局API异常
    /// </summary>
    public class WebApiExceptionFilter : ExceptionFilterAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public override void OnException(HttpActionExecutedContext context)
        {
            var ex = context.Exception;
            if(ex is ResultException)
            {
                var ex2 = (ResultException)ex;
                context.Response = context.Request.CreateResponse(ex2.ToResult());
            }
            base.OnException(context);
        }
    }

只是这样抛异常 对性能有影响吗?