static string GetResponseString(HttpContext context) { MornRequestData requestData = MornRequestData.Create(context); if (ProtocolDataVerifier != null) { try { ProtocolDataVerifier.Validate(requestData); } catch (MornException ex) { return(MornErrorUtil.GenError( ex.ErrorType, ex.Message)); } } var handler = GetProtocolHandler(requestData.Method); if (handler == null) { return(MornErrorUtil.GenError(MornErrorType.UnknownMethod)); } return(handler.Process(requestData)); }
/// <summary> /// 生成错误回应字符串 /// </summary> /// <param name="type">错误类型</param> /// <returns>错误回应字符串</returns> public static string GenError(MornErrorType type) { return(JsonConvert.SerializeObject(new Dictionary <string, object>() { { MornConstants.ERROR_CODE, MornErrorUtil.GetErrorCode(type) }, { MornConstants.ERROR_MESSAGE, MornErrorUtil.GetDescription(type) }, })); }
public string Process(MornRequestData data) { try { return(_client.DoPost(_url, data.Context)); } catch (Exception ex) { return(MornErrorUtil.GenError(MornErrorType.SystemError, ex.Message)); } }
private T CreateErrorResponse <T>(MornErrorType type, string addition) where T : MornResponse { var rsp = Activator.CreateInstance <T>(); rsp.ErrorCode = MornErrorUtil.GetErrorCode(type); if (string.IsNullOrEmpty(addition)) { rsp.ErrorMessage = MornErrorUtil.GetDescription(type); } else { rsp.ErrorMessage = string.Format("{0} : {1}", MornErrorUtil.GetDescription(type), addition); } return(rsp); }
public static async Task ProcessHttpContextAsync(HttpContext context) { string responseString = string.Empty; try { responseString = GetResponseString(context); } catch (Exception ex) { responseString = MornErrorUtil.GenError( MornErrorType.SystemError, ex.Message); } context.Response.ContentType = "application/json;charset=UTF-8"; await context.Response.WriteAsync(responseString); }