4
1

public IHttpActionResult AddItem([FromUri] string name)
{
    try
    {
        // call service method
        return this.Ok();
    }
    catch (MyException1)
    {
        return this.NotFound();
    }
    catch (MyException2 e)
    {
        return this.Content(HttpStatusCode.Conflict, e.Message);
    }
}

{
  "message": "here is your error msg"
}
|

4

2

public IHttpActionResult AddItem([FromUri] string name) {
    try {
        // call service method
        return this.Ok();
    } catch (MyException1) {
        return this.NotFound();
    } catch (MyException2 e) {
        var error = new { message = e.Message }; //<-- anonymous object
        return this.Content(HttpStatusCode.Conflict, error);
    }
}
|
    1

    public class TestingMessage
    {
        [JsonProperty("message")]
        public string message{ get; set; }
    }
    
    public IHttpActionResult AddItem([FromUri] string name)
    {
        TestingMessage errormsg=new TestingMessage();
        try
        {
            // call service method
            return this.Ok();
        }
        catch (MyException1)
        {
            return this.NotFound();
        }
        catch (MyException2 e)
        {
            string error=this.Content(HttpStatusCode.Conflict, e.Message);
            errormsg.message=error;
            return errormsg;
        }
    }
    
    |
      0

      1) the easiest way: You can return directly whichever object you want, and it will be serialized as JSON. It can even be an anonymous class object created with new { }

      return new HttpResponseMessage(HttpStatusCode.BadRequest)
          {
              Content = new ObjectContent(typeof(ErrorClass), errors, new JsonMediaTypeFormatter())
          };
      
      |
        0
        return Json(new {message = e.Message});
        
        |