public JsonResult GetCategoryById(int id) { APN_Category apnCategory = null; using (var client = new HttpClient()) { client.BaseAddress = new Uri(String.Concat(baseUrl, "api/Category")); var responseTask = client.GetAsync("Category?id=" + id.ToString()); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsAsync <APN_Category>(); readTask.Wait(); apnCategory = readTask.Result; } else { apnCategory = null; ModelState.AddModelError(string.Empty, "Server Error. Please contact administrator."); } } return(Json(apnCategory, JsonRequestBehavior.AllowGet)); }
public async Task <IHttpActionResult> PutAPN_Category(int id, APN_Category aPN_Category) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != aPN_Category.id) { return(BadRequest()); } db.Entry(aPN_Category).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!APN_CategoryExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> GetAPN_Category(int id) { APN_Category aPN_Category = await db.APN_Category.FindAsync(id); if (aPN_Category == null) { return(NotFound()); } return(Ok(aPN_Category)); }
public async Task <IHttpActionResult> PostAPN_Category(APN_Category aPN_Category) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.APN_Category.Add(aPN_Category); await db.SaveChangesAsync(); return(CreatedAtRoute("DefaultApi", new { id = aPN_Category.id }, aPN_Category)); }
// POST: api/APN_Category public HttpResponseMessage Post([FromBody] APN_Category category) { try { categorys.SaveData(category); return(Request.CreateResponse(HttpStatusCode.Created, category.name)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public async Task <IHttpActionResult> DeleteAPN_Category(int id) { APN_Category aPN_Category = await db.APN_Category.FindAsync(id); if (aPN_Category == null) { return(NotFound()); } db.APN_Category.Remove(aPN_Category); await db.SaveChangesAsync(); return(Ok(aPN_Category)); }
// PUT: api/APN_Category/5 public HttpResponseMessage Put(int id, [FromBody] APN_Category category) { try { var entity = categorys.GetUniqueData(id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "category with id " + id + " not found to update")); } else { categorys.UpdateRecord(id, category); return(Request.CreateResponse(HttpStatusCode.OK, "category with id " + id + " update successfully..")); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public ActionResult Create(APN_Category category) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(String.Concat(baseUrl, "api/APN_Category")); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var resTask = client.PostAsJsonAsync <APN_Category>("Category", category); resTask.Wait(); var result = resTask.Result; if (result.IsSuccessStatusCode) { return(RedirectToAction("Index")); } return(View(category)); } }