Exemple #1
0
        //inserting values using Url or in a single querry string
        //http://localhost:49965/api/UriTest?Sno=1&Ename=FN2&Ephone=9999&Eaddress=biharipur
        public HttpResponseMessage Put(int Sno, [FromUri] ApiTesting employee)
        {
            try
            {
                using (testingEntities entities = new testingEntities())
                {
                    var entity = entities.ApiTestings.FirstOrDefault(e => e.Sno == Sno);
                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                           "Employee with Id " + Sno.ToString() + " not found to update"));
                    }
                    else
                    {
                        entity.Ename    = employee.Ename;
                        entity.Ephone   = employee.Ephone;
                        entity.Eaddress = employee.Eaddress;


                        entities.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK, entity));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
        //Post Method with Location of Inserted Record
        public HttpResponseMessage Post([FromBody] ApiTesting apiObject)
        {
            try
            {
                using (testingEntities entities = new testingEntities())
                {
                    entities.ApiTestings.Add(apiObject);
                    entities.SaveChanges();

                    var message = Request.CreateResponse(HttpStatusCode.Created, apiObject);
                    message.Headers.Location = new Uri(Request.RequestUri + apiObject.Sno.ToString());

                    return(message);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }