请问一下,webAPI怎样做到统一接口调用?

karl122 2017-11-30 02:35:14
我们开发一个对外接口。想对外统一的接口和错误处理接口。如何实现
vs2013.希望大神给出思路和方法。谢谢
...全文
884 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 2017-11-30
  • 打赏
  • 举报
回复
http://blog.csdn.net/hanjun0612/article/details/50913485
by_封爱 2017-11-30
  • 打赏
  • 举报
回复
拦截器被 如果错了 就输出固定的json 这网上有很多..,

 public override void OnActionExecuted

if (actionExecutedContext.Exception != null)
            {


这里写就行了
圣殿骑士18 2017-11-30
  • 打赏
  • 举报
回复
案例: 1、出代码出错时都抛出异常 2、定义规范的反馈消息对象(AjaxResult) 3、自定义ExceptionFilterAttribute,处理异常并以规范的(WebApiExceptionFilterAttribute)

public class WebApiExceptionFilterAttribute : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext context)
        {
            base.OnException(context);
            ResultType resultType = ResultType.error;
            string showMessage = string.Empty;
            string errorMessage = string.Empty;
            if (context.Exception is ShowInfoException)
            {
                resultType = ResultType.info;
            }
            else if (context.Exception is ShowWarningException)
            {
                resultType = ResultType.warning;
            }

            //异常消息处理
            if (context.Exception is ShowException)
            {
                //显示类异常:在客户端提示
                showMessage = context.Exception.Message;
            }
            else
            {
                //其他类Exception:优化并记录日志
                ExceptionService exService = new ExceptionService();
                exService.ReSolveEFException += EFExceptionService.ReSolveEFException;
                errorMessage = exService.Resolve(context.Exception, "Resolve Exception").ToLower().Replace(BaseManager.ProjectPath.ToLower(), "");

                string errorMessageAdd = Environment.NewLine + "日志时间: " + DateTime.Now.ToString(BaseFormat.DateTimeFormat) + Environment.NewLine + errorMessage;
                var log = LogFactory.GetLogger(LogFactory.Error);
                log.Error(new { errorMessage = errorMessageAdd, Exception = context.Exception });

                //显示类异常:在客户端提示
                if (context.Exception is CustomShowException)
                {
                    //CustomShowException异常特殊处理,它在记入日志的同时,也要反馈到客户端
                    showMessage = context.Exception.Message;
                }
                else
                {
                    //其他 可格式化 的系统异常:对确定性的错误进行匹配
                    string matchStr = ExceptionMatch.Match(errorMessage);
                    showMessage = matchStr != null ? matchStr : "内部服务器错误,请联系管理员。";  //无法匹配的,以简单的错误消息返回
                }
            }

            //var response = new HttpResponseMessage(HttpStatusCode.InternalServerError);

            context.Response = new HttpResponseMessage()
            {
                Content = new StringContent(new AjaxResult { state = resultType.ToString(), message = showMessage }.ToJson())
            };
        }
    }
4、将WebApiExceptionFilterAttribute 注册到全局

public static void Register(HttpConfiguration config)
        {
            //缺省路由
            config.Routes.MapHttpRoute(
                name: AppManager.AppId + "Api",
                routeTemplate: "api/{controller}/{action}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Filters.Add(new WebApiExceptionFilterAttribute());
        }

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧