I use the following code to send an error message to a client from a filter (ActionFilterAttribute).
catch (Exception) { var response = context.Request.CreateResponse(httpStatusCode.Unauthorized); response.Content = new StringContent("User with api key is not valid"); context.Response = response; }
But the problem is that it is sent only in plain text. I wanted to send it as the current formatting format. Like in json or xml form.
Here I know that this is because I use StringContent (). But how can we write using a custom Error object? For example, the following does not work:
response.Content = new Error({Message = "User with api key is not valid"});
How do we write the code for this? Thanks in advance.
source share