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 "error":"ROOM cannot be Empty or NULL" if the ROOM in URI is empty like ROOM=&DOB_GT=01-SEP-05&DOB_LT=30-DEC-06&STATUS_TYPE=CMPLT
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<OracleParameter> prms = new List<OracleParameter>();
List<string> selectionStrings = new List<string>();
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 > :DOB_GT and DOB < :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;
}
With my code above it does return like ROOM cannot be Empty or NULL how to get like "error":"ROOM cannot be Empty or NULL". Also is there any way to handle error in the URLI and return the JSON response as "error":"Poorly Formed URI"