Web Api 2: BadRequest with custom error model

The BadRequest method available in ApiController accepts only a string:

 // Summary: // Creates an System.Web.Http.Results.ErrorMessageResult (400 Bad Request) with // the specified error message. // // Parameters: // message: // The user-visible error message. // // Returns: // An System.Web.Http.Results.InvalidModelStateResult with the specified model // state. protected internal virtual BadRequestErrorMessageResult BadRequest(string message); 

Why is there no overload that accepts a custom T error model?

For example, I may need to return the code along with the message:

 { message: "Error message", code: "1000", } 

I could just do the following, but I suspect there should be a main reason (perhaps according to RESTful standards?):

 return Content(HttpStatusCode.BadRequest, errorModel); 
+6
source share
1 answer

Not sure if this is still relevant since the question is more than two years old ... However, the way to do this is really using

 return Content(HttpStatusCode.BadRequest, errorModel); 

Having a standardized error model to include detailed information is actually pretty good practice. See Also Here: API Recommendations: Response Processing

+1
source

Source: https://habr.com/ru/post/979489/


All Articles