Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

model - How can I customize the error response in Web API with .NET Core?

I am using .NET Core 2.2 with Web API. I have created one class, i.e., as below:

public class NotificationRequestModel
{
    [Required]
    public string DeviceId { get; set; }
    [Required]
    public string FirebaseToken { get; set; }
    [Required]
    public string OS { get; set; }

    public int StoreId { get; set; }
}

Using the above class I have created one method. Now I want to return a custom object, but it's returning its own object. API method is:

public ActionResult<bool> UpdateFirebaseToken(NotificationRequestModel model)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(FormatOutput(ModelState.Values));
    }
    var result = _notificationService.InsertOrUpdateFirebaseToken(model);
    return Ok(result);
}

Here FormatOutput method is format the output.

protected Base FormatOutput(object input, int code = 0, string message = "", string[] details = null)
{
    Base baseResult = new Base();
    baseResult.Status = code;
    baseResult.Error = message;
    baseResult.TimeStamp = CommonHelper.CurrentTimeStamp;
    baseResult.Code = code;
    baseResult.Details = details;
    baseResult.Message = message; //Enum.Parse<APIResponseMessageEnum>(code.ToString(), true); // (enum of code get value from language)
    return baseResult;
}

But the issue is it returns:

{
  "errors": {
    "DeviceId": [
      "The DeviceId field is required."
    ]
  },
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "80000049-0001-fc00-b63f-84710c7967bb"
}

I want to customize this error with my model. I need error message and details from return output and passed it to my model. How can I do that? I had try to debug my code and found that breakpoint on API method is not calling. So I can't handle my custom method. Is there any solution? What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When using a controller with the ApiController attribute applied, ASP.NET Core automatically handles model validation errors by returning a 400 Bad Request with ModelState as the response body. As such, your conditional testing ModelState.IsValid is essentially always false (and therefore not entered) because the only requests that will ever get this far are valid ones.

You could simply remove the ApiController attribute, but that removes a bunch of other beneficial stuff the attributes adds as well. The better option is to use a custom response factory:

services.Configure<ApiBehaviorOptions>(o =>
{
    o.InvalidModelStateResponseFactory = actionContext =>
        new BadRequestObjectResult(actionContext.ModelState);
});

That's essentially what's happening by default, so you'd simply need to change the action provided there accordingly to customize it to your whims.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...