//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)); } }
// Delete of Database Entry public HttpResponseMessage Delete(int id) { try { using (testingEntities entities = new testingEntities()) { var entity = entities.ApiTestings.FirstOrDefault(e => e.Sno == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id = " + id.ToString() + " not found to delete")); } else { entities.ApiTestings.Remove(entity); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
[HttpPost]//annotation public ActionResult Index(abc b) { db.abcs.Add(b); int s = db.SaveChanges(); ModelState.Clear(); if (s > 0) { ViewBag.msg = "inserted successfully"; } else { ViewBag.msg = "inserted unsuccessfully"; } return(RedirectToAction("Select", "Home")); }
//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)); } }