How to replace 400 bad request error message with a custom message

Viewed 607

I am creating a webapi and just wondering whether I can replace the default error message in case of 400 Bad Request.

For example, in this url (http://localhost/api/values/4) 4 is the integer type but if I pass a string intentionally (e.g. http://localhost:35453/api/values/sss), I receive an error (400 Bad Request) in the following format:

{ "Message": "The request is invalid.", "MessageDetail": "The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Http.IHttpActionResult Get(Int32)' in 'WebAp__DELETE.Controllers.ValuesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter." }

Is there any way I can replace this default error message with a custom error message of mine.

0 Answers
Related