i am using the following code to send out error message to 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 problem is that it sends out in plain text only. I wanted to send it as format of current formatter. Like in form of json or xml.
Here i know that this is because i am using StringContent(). But how can we write using custom Error object? like, the following is not working either:
response.Content = new Error({Message = "User with api key is not valid"});
How do we write code for this? Thanks in advance.
Ya, i found the correct syntax of writing. We can write like:
catch (Exception)
{
var response = context.Request.CreateResponse(HttpStatusCode.NotFound,
new Error { Message = "User with api key is not valid"});
context.Response = response;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.