title: Return JSON when Handling error in Web API
body: <p>I have created the Web API that accepts four input parameters that is used in Querying Oracle Database and returns result in the JSON format. Now I am trying to handle any exception in the URI if there is any input parameter missing or in the wrong format . Like returning in JSON <code>"error":"ROOM cannot be Empty or NULL"</code> if the ROOM in URI is empty like <code>ROOM=&amp;DOB_GT=01-SEP-05&amp;DOB_LT=30-DEC-06&amp;STATUS_TYPE=CMPLT</code></p>
  
  <pre><code>public class TGSDataController : ApiController
  {
  [HttpGet]
  public HttpResponseMessage Getdetails(string ROOM, DateTime DOB_GT, DateTime DOB_LT, string STATUS_TYPE)
  {
      if (string.IsNullOrEmpty(ROOM))
      {
          var resp = new HttpResponseMessage()
          {
              Content = new StringContent("ROOM cannot be Empty or NULL")
          };
          resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
          return resp;
      }
      List&lt;OracleParameter&gt; prms = new List&lt;OracleParameter&gt;();
      List&lt;string&gt; selectionStrings = new List&lt;string&gt;();
      prms.Add(new OracleParameter("ROOM", OracleDbType.Varchar2, ROOM, ParameterDirection.Input));
      prms.Add(new OracleParameter("DOB_GT", OracleDbType.Date, DOB_GT, ParameterDirection.Input));
      prms.Add(new OracleParameter("DOB_LT", OracleDbType.Date, DOB_LT, ParameterDirection.Input));
      prms.Add(new OracleParameter("STATUS_TYPE", OracleDbType.Varchar2, STATUS_TYPE, ParameterDirection.Input));
  
      string connStr = ConfigurationManager.ConnectionStrings["TGSDataBaseConnection"].ConnectionString;
      using (OracleConnection dbconn = new OracleConnection(connStr))
      {
          DataSet userDataset = new DataSet();
          var strQuery = "SELECT * from LIMS_SAMPLE_RESULTS_VW where ROOM = :ROOM and DOB &gt; :DOB_GT and DOB &lt; :DOB_LT and STATUS_TYPE= :STATUS_TYPE ";
  
          var returnObject = new { data = new OracleDataTableJsonResponse(connStr, strQuery, prms.ToArray()) };
          var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json"));
          ContentDispositionHeaderValue contentDisposition = null;
          if (ContentDispositionHeaderValue.TryParse("inline; filename=TGSData.json", out contentDisposition))
          {
              response.Content.Headers.ContentDisposition = contentDisposition;
          }
          return response;
      }
  </code></pre>
  
  <p>With my code above it does return like ROOM cannot be Empty or NULL how to get like <code>"error":"ROOM cannot be Empty or NULL"</code>. Also is there any way to handle error in the URLI and return the JSON response as <code>"error":"Poorly Formed URI"</code></p>