コード例 #1
0
ファイル: BaseMidleware.cs プロジェクト: KevinWG/OSS.Core
        /// <summary>
        ///  清理Response缓存
        /// </summary>
        /// <param name="httpResponse"></param>
        /// <param name="res"></param>
        protected static Task ResponseJsonError(HttpResponse httpResponse, IResp res)
        {
            httpResponse.Clear();
            httpResponse.Headers.Remove("ETag");

            httpResponse.Headers["Pragma"]        = "no-cache";
            httpResponse.Headers["Expires"]       = "-1";
            httpResponse.Headers["Cache-Control"] = "no-cache";

            httpResponse.ContentType = "application/json; charset=utf-8";
            httpResponse.StatusCode  = (int)HttpStatusCode.OK;
            return(httpResponse.WriteAsync($"{{\"ret\":{res.ret},\"msg\":\"{res.msg}\"}}"));
        }
コード例 #2
0
        /// <summary>
        ///  处理方法
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task Invoke(HttpContext context)
        {
            Exception error     = null;
            IResp     errorResp = null;

            // 需要在此初始化,否则中间件依次退出后此值为空,下方异常无法捕获APP信息
            var appInfo = context.InitialCoreAppIdentity();

            try
            {
                await _next.Invoke(context);

                if (context.Response.StatusCode == (int)HttpStatusCode.NotFound)
                {
                    await ExceptionResponse(context, appInfo, new Resp(RespTypes.OperateObjectNull, "当前请求资源不存在!"));
                }

                return;
            }
            catch (RespException resEx)
            {
                errorResp = resEx;
                error     = resEx;
            }
            catch (Exception ex)
            {
                error = ex;
            }

            var code = LogHelper.Error(string.Concat("请求地址:", context.Request.Path, "错误信息:", error.Message, "详细信息:", error.StackTrace),
                                       nameof(CoreExceptionMiddleware));

#if DEBUG
            if (error != null)
            {
                throw error;
            }
#endif
            var res = errorResp ?? new Resp(SysRespTypes.AppError, $"当前服务异常({ error.Message})!");
            await ExceptionResponse(context, appInfo, res);
        }
コード例 #3
0
        /// <summary>
        ///  异常响应处理
        /// </summary>
        /// <param name="context"></param>
        /// <param name="appInfo"></param>
        /// <param name="res"></param>
        /// <returns></returns>
        private static Task ExceptionResponse(HttpContext context, AppIdentity appInfo, IResp res)
        {
            var url = InterReqHelper.GetErrorPage(context, appInfo, res);

            if (string.IsNullOrEmpty(url))
            {
                return(ResponseJsonError(context.Response, res));
            }
            context.Response.Redirect(url);
            return(Task.CompletedTask);
        }
コード例 #4
0
ファイル: InterReqHelper.cs プロジェクト: KevinWG/OSS.Core
        internal static string GetErrorPage(HttpContext context, AppIdentity appInfo, IResp res)
        {
            if (appInfo.source_mode != AppSourceMode.Browser || context.Request.IsFetchApi())
            {
                return(string.Empty);
            }

            if (CheckIfErrorUrl(context.Request.Path.ToString()))
            {
                return(string.Empty);
            }

            var errUrl = Option?.ErrorPage;

            return(string.IsNullOrEmpty(errUrl)
                ? string.Empty
                : string.Concat(errUrl, "?ret=", res.ret, "&msg=", errUrl.SafeEscapeUriDataString()));
        }