如何显示Httperror的响应列表



我使用以下代码显示错误消息

var errorMessagError = new HttpError("Type Code does not exist") { { "Code", "CT-1" } };
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, errorMessagError));

和邮递员中的输出如下。

{
  "error": {
    "code": "CT-1",
    "message": "Type Code does not exist"
  }
}

但是我需要在下面显示多个错误消息。

{
  "error": {
    "code": "CT-1",
    "message": "User is not Authenticated."
  },
  "error": {
    "code": "CT-2",
    "message": "User is not Authorized."
  },
  "error": {
    "code": "CT-3",
    "message": "User is not Valid."
  }
}

我们有什么方法可以实现这一目标?

我使用odataerror显示了多个子错误消息,如

`

   public HttpResponseMessage ShowErrors(List<TargetErrorCollection> childErrorColletion )
    {
       ODataError errors = new ODataError();
        errors.ErrorCode = "CT-01";
        errors.Message = "Parent details";
        errors.Details = new Collection<ODataErrorDetail>();
        ODataErrorDetail childErr= new ODataErrorDetail();
        foreach (var err in childErrorColletion)
        {
            childErr.ErrorCode = err.MessageCode;
            childErr.Target = err.Target;
            childErr.Message = err.Message;
            errors.Details.Add(childErr);
        }   
        return Request.CreateResponse(HttpStatusCode.BadRequest, errors);
    }

`输出看起来像

{ "error": { "code": "CT-01", "message": "Data cannot be inserted", "details": [ { "code": "CT-02", "target": "Code", "message": "Invalid value entered" }, { "code": "CT-03", "target": "DataType", "message": "Invalid value entered" } ] } }

相关内容

  • 没有找到相关文章

最新更新