Advertisement
Guest User

Untitled

a guest
Feb 1st, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. Food.aspx
  2.  
  3. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Food.aspx.cs" Inherits="MySwoleMate300WebApi.Food" %>
  4.  
  5. <!DOCTYPE html>
  6.  
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head runat="server">
  9. <title></title>
  10. </head>
  11. <body>
  12. <form id="form1" runat="server">
  13. <div>
  14. <asp:GridView ID="FoodList" runat="server" AutoGenerateColumns="False" DataKeyNames="FoodID">
  15. <Columns>
  16. <asp:BoundField DataField="FoodName" HeaderText="Food Name" />
  17. <asp:BoundField DataField="Calories" HeaderText="Calories" />
  18. <asp:BoundField DataField="Notes" HeaderText="Notes" />
  19. </Columns>
  20. </asp:GridView>
  21. </div>
  22. </form>
  23. </body>
  24. </html>
  25.  
  26. Food.aspx.cs
  27.  
  28. using Newtonsoft.Json;
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using System.Net.Http;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Web.UI.WebControls;
  36.  
  37. namespace MySwoleMate300WebApi
  38. {
  39. public partial class Food : System.Web.UI.Page
  40. {
  41. protected void Page_Load(object sender, EventArgs e)
  42. {
  43. HttpClient client = new HttpClient();
  44.  
  45. client.BaseAddress = new Uri("http://localhost:1433/");
  46. client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
  47.  
  48. HttpResponseMessage response = client.GetAsync("api/foods").Result;
  49.  
  50. if (response.IsSuccessStatusCode)
  51. {
  52. string foodstring = response.Content.ReadAsStringAsync().Result;
  53. FoodList.DataSource = JsonConvert.DeserializeObject<List<Food>>(foodstring);
  54. FoodList.DataBind();
  55. }
  56. }
  57.  
  58. }
  59. }
  60.  
  61. FoodController.cs
  62.  
  63. using System;
  64. using System.Collections.Generic;
  65. using System.Data;
  66. using System.Data.Entity;
  67. using System.Data.Entity.Infrastructure;
  68. using System.Linq;
  69. using System.Net;
  70. using System.Net.Http;
  71. using System.Web.Http;
  72. using System.Web.Http.Description;
  73. using MySwoleMate300WebApi;
  74.  
  75. namespace MySwoleMate300WebApi.Controllers
  76. {
  77. public class FoodsController : ApiController
  78. {
  79. private MySwoleMateEntities db = new MySwoleMateEntities();
  80.  
  81. // GET: api/Foods
  82. public IQueryable<Food> GetFoods()
  83. {
  84. return db.Foods;
  85. }
  86.  
  87. // GET: api/Foods/5
  88. [ResponseType(typeof(Food))]
  89. public IHttpActionResult GetFood(int id)
  90. {
  91. Food food = db.Foods.Find(id);
  92. if (food == null)
  93. {
  94. return NotFound();
  95. }
  96.  
  97. return Ok(food);
  98. }
  99.  
  100. // PUT: api/Foods/5
  101. [ResponseType(typeof(void))]
  102. public IHttpActionResult PutFood(int id, Food food)
  103. {
  104. if (!ModelState.IsValid)
  105. {
  106. return BadRequest(ModelState);
  107. }
  108.  
  109. if (id != food.FoodID)
  110. {
  111. return BadRequest();
  112. }
  113.  
  114. db.Entry(food).State = EntityState.Modified;
  115.  
  116. try
  117. {
  118. db.SaveChanges();
  119. }
  120. catch (DbUpdateConcurrencyException)
  121. {
  122. if (!FoodExists(id))
  123. {
  124. return NotFound();
  125. }
  126. else
  127. {
  128. throw;
  129. }
  130. }
  131.  
  132. return StatusCode(HttpStatusCode.NoContent);
  133. }
  134.  
  135. // POST: api/Foods
  136. [ResponseType(typeof(Food))]
  137. public IHttpActionResult PostFood(Food food)
  138. {
  139. if (!ModelState.IsValid)
  140. {
  141. return BadRequest(ModelState);
  142. }
  143.  
  144. db.Foods.Add(food);
  145. db.SaveChanges();
  146.  
  147. return CreatedAtRoute("DefaultApi", new { id = food.FoodID }, food);
  148. }
  149.  
  150. // DELETE: api/Foods/5
  151. [ResponseType(typeof(Food))]
  152. public IHttpActionResult DeleteFood(int id)
  153. {
  154. Food food = db.Foods.Find(id);
  155. if (food == null)
  156. {
  157. return NotFound();
  158. }
  159.  
  160. db.Foods.Remove(food);
  161. db.SaveChanges();
  162.  
  163. return Ok(food);
  164. }
  165.  
  166. protected override void Dispose(bool disposing)
  167. {
  168. if (disposing)
  169. {
  170. db.Dispose();
  171. }
  172. base.Dispose(disposing);
  173. }
  174.  
  175. private bool FoodExists(int id)
  176. {
  177. return db.Foods.Count(e => e.FoodID == id) > 0;
  178. }
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement