Beispiel #1
0
        public async Task Invoke(DownstreamContext context)
        {
            await _next.Invoke(context);

            if (context.IsError)
            {
                //  Logger.LogWarning($"{context.Errors.ToErrorString()} errors found in {MiddlewareName}. Setting error response for request path:{context.HttpContext.Request.Path}, request method: {context.HttpContext.Request.Method}");

                // SetErrorResponse(context.HttpContext, context.Errors);

                var errmsg     = context.Errors[0].Message;
                int httpstatus = _codeMapper.Map(context.Errors);
                var errResult  = new ErrorResult()
                {
                    errcode = httpstatus, errmsg = errmsg
                };
                var message = errResult.ToJson();
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
                await context.HttpContext.Response.WriteAsync(message);

                return;
            }
            else if (context.DownstreamResponse == null)
            {//添加如果管道强制终止,不做任何处理,修复未将对象实例化错误
            }
            else
            {
                Logger.LogDebug("no pipeline errors, setting and returning completed response");
                await _responder.SetResponseOnHttpContext(context.HttpContext, context.DownstreamResponse);
            }
        }
Beispiel #2
0
        private static void WriteErrorResponse(HttpContext context, int code, IEnumerable <string> errors)
        {
            var response = new ErrorResult(errors, context.TraceIdentifier);

            context.Response.StatusCode  = code;
            context.Response.ContentType = "application/json";
            context.Response.WriteAsync(response.ToJson());
        }
 /// <summary>
 /// Action执行之前执行
 /// </summary>
 /// <param name="filterContext">过滤器上下文</param>
 public void OnActionExecuting(ActionExecutingContext filterContext)
 {
     if (filterContext.HttpContext.Request.HttpMethod.ToLower() != "post")
     {
         ErrorResult res = new ErrorResult
         {
             Msg = "仅支持POST方法"
         };
         filterContext.Result = new ContentResult {
             Content = res.ToJson(), ContentEncoding = Encoding.UTF8
         };
     }
 }
        /// <summary>
        /// Invokes the next middleware of the current request hontext
        /// </summary>
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception ex)
            {
                if (!context.Response.HasStarted)
                {
                    int errorCode = (int)HttpStatusCode.InternalServerError;

                    context.Response.Clear();
                    context.Response.StatusCode  = errorCode;
                    context.Response.ContentType = "application/json";

                    ErrorResult error = new ErrorResult(errorCode, "Unexpected internal error", ex.GetType().Name);
                    await context.Response.WriteAsync(error.ToJson());
                }
            }
        }