Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Food.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Food.aspx.cs" Inherits="MySwoleMate300WebApi.Food" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="FoodList" runat="server" AutoGenerateColumns="False" DataKeyNames="FoodID">
- <Columns>
- <asp:BoundField DataField="FoodName" HeaderText="Food Name" />
- <asp:BoundField DataField="Calories" HeaderText="Calories" />
- <asp:BoundField DataField="Notes" HeaderText="Notes" />
- </Columns>
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
- Food.aspx.cs
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace MySwoleMate300WebApi
- {
- public partial class Food : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri("http://localhost:1433/");
- client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
- HttpResponseMessage response = client.GetAsync("api/foods").Result;
- if (response.IsSuccessStatusCode)
- {
- string foodstring = response.Content.ReadAsStringAsync().Result;
- FoodList.DataSource = JsonConvert.DeserializeObject<List<Food>>(foodstring);
- FoodList.DataBind();
- }
- }
- }
- }
- FoodController.cs
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using System.Web.Http.Description;
- using MySwoleMate300WebApi;
- namespace MySwoleMate300WebApi.Controllers
- {
- public class FoodsController : ApiController
- {
- private MySwoleMateEntities db = new MySwoleMateEntities();
- // GET: api/Foods
- public IQueryable<Food> GetFoods()
- {
- return db.Foods;
- }
- // GET: api/Foods/5
- [ResponseType(typeof(Food))]
- public IHttpActionResult GetFood(int id)
- {
- Food food = db.Foods.Find(id);
- if (food == null)
- {
- return NotFound();
- }
- return Ok(food);
- }
- // PUT: api/Foods/5
- [ResponseType(typeof(void))]
- public IHttpActionResult PutFood(int id, Food food)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- if (id != food.FoodID)
- {
- return BadRequest();
- }
- db.Entry(food).State = EntityState.Modified;
- try
- {
- db.SaveChanges();
- }
- catch (DbUpdateConcurrencyException)
- {
- if (!FoodExists(id))
- {
- return NotFound();
- }
- else
- {
- throw;
- }
- }
- return StatusCode(HttpStatusCode.NoContent);
- }
- // POST: api/Foods
- [ResponseType(typeof(Food))]
- public IHttpActionResult PostFood(Food food)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- db.Foods.Add(food);
- db.SaveChanges();
- return CreatedAtRoute("DefaultApi", new { id = food.FoodID }, food);
- }
- // DELETE: api/Foods/5
- [ResponseType(typeof(Food))]
- public IHttpActionResult DeleteFood(int id)
- {
- Food food = db.Foods.Find(id);
- if (food == null)
- {
- return NotFound();
- }
- db.Foods.Remove(food);
- db.SaveChanges();
- return Ok(food);
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- private bool FoodExists(int id)
- {
- return db.Foods.Count(e => e.FoodID == id) > 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement