コード例 #1
0
        protected void Application_Error(object sender, EventArgs e)
        {
            if (AppConfig.Debug) return;
            var exception = Server.GetLastError().GetBaseException();
            var apiException = exception as ApiException;
            var ret = new ApiModel();
            if (apiException == null) ret.Messages.Add(new Notification { Content = exception.Message, MessageType = Notification.Type.Error });
            else ret.Messages.AddRange(apiException.ErrorStrings.Select(x => new Notification { Content = x, MessageType = Notification.Type.Error }));

            Response.StatusCode = 400;
            Response.ContentType = "application/json";
            Response.Write(JsonConvert.SerializeObject(ret));
            Server.ClearError();
        }
コード例 #2
0
ファイル: WebApiConfig.cs プロジェクト: ron-liu/webApiAngular
        public override void Handle(ExceptionHandlerContext context)
        {
            var negotiator = context.RequestContext.Configuration.Services.GetService(typeof(IContentNegotiator)) as IContentNegotiator;
            var formatters = context.RequestContext.Configuration.Formatters;

            var exception = context.Exception as ApiException;
            var ret       = new ApiModel();

            if (exception == null)
            {
                ret.Messages.Add(new Notification {
                    Content = context.Exception.Message, MessageType = Notification.Type.Error
                });
            }
            else
            {
                ret.Messages.AddRange(exception.ErrorStrings.Select(x => new Notification {
                    Content = x, MessageType = Notification.Type.Error
                }));
            }

            context.Result = new NegotiatedContentResult <ApiModel>(exception != null ? HttpStatusCode.BadRequest : HttpStatusCode.InternalServerError, ret, negotiator, context.Request, formatters);
            base.Handle(context);
        }
コード例 #3
0
        public override void Handle(ExceptionHandlerContext context)
        {
            var negotiator = context.RequestContext.Configuration.Services.GetService(typeof(IContentNegotiator)) as IContentNegotiator;
            var formatters = context.RequestContext.Configuration.Formatters;

            var exception = context.Exception as ApiException;
            var ret = new ApiModel();
            if (exception == null) ret.Messages.Add(new Notification { Content = context.Exception.Message, MessageType = Notification.Type.Error });
            else ret.Messages.AddRange(exception.ErrorStrings.Select(x => new Notification { Content = x, MessageType = Notification.Type.Error }));

            context.Result = new NegotiatedContentResult<ApiModel>(exception != null ? HttpStatusCode.BadRequest : HttpStatusCode.InternalServerError, ret, negotiator, context.Request, formatters);
            base.Handle(context);
        }