/// <summary> /// /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task Invoke(HttpContext context) { try { await _next.Invoke(context); } catch (ApiException ex) { var result = new ApiData { Result = new ApiStatus { Code = ex.StatusCode, Msg = ex.Message } }; context.Response.ContentType = "application/json;charset=utf-8"; await context.Response.WriteAsync(result.ToApiJson() ?? string.Empty); } catch (Exception ex) { Logger.WriteLog(LogLevel.Error, "服务器异常", ex); var result = new ApiData { Result = new ApiStatus { Code = -1, Msg = "服务器异常" } }; context.Response.ContentType = "application/json;charset=utf-8"; await context.Response.WriteAsync(result.ToApiJson() ?? string.Empty); } }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <returns></returns> public virtual async Task ExecuteResultAsync(ActionContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var apiData = new ApiData { Result = Result }; context.HttpContext.Response.ContentType = "application/json;charset=utf-8"; await context.HttpContext.Response.WriteAsync(apiData.ToApiJson() ?? string.Empty); }