Bir Restful WebApi sınıfı kullanmak için Visual Studio - C# konsol uygulaması - istemcisi oluşturdum. Kayıtların eklenmesi, güncellenmesi, silinmesi ve 1 kaydın görüntülenmesi yanında görüntülenen kayıtların TÜMÜNÜ görüntülemeye çalışıyorum. Sadece ürünler tablosundaki TÜM kayıtların görüntülenmesi gerekiyor.
Restful WebApi sınıfımda, veritabanını tohumlamak için geçişler kullanıyorum. Taşımacılık klasörümde configuration.cs dosyasını değiştirdim.WebApi İstemci Restful WebApi sınıfını tüketecek - Tüm TÜM kayıtları göster
protected override void Seed(RestfulWebAPIExample.Models.AppDbContext context)
{
context.Products.AddOrUpdate(p => p.ID,
new Product { Name = "Milk", Price = 3.99m, Quantity = 6 },
new Product { Name = "Eggs", Price = 4.99m, Quantity = 12 },
new Product { Name = "Cheese", Price = 6.99m, Quantity = 7 },
new Product { Name = "Bacon", Price = 9.99m, Quantity = 9 }
);
}
müvekkilemle Ürün Modeli sınıf var Tohum yönteminde aşağıdaki kodu eklendi Burada
namespace RestfulWebAPIExample
{
class Product
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
}
Ben istemci benim program.cs var kod bazı edilir - gösterilecek
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace WebAPIExampleClient
{
class Program
{
static void Main()
{
RunAsync().Wait();
}
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP GET
HttpResponseMessage response = await client.GetAsync("api/products/1");
if (response.IsSuccessStatusCode)
{
Product product = await response.Content.ReadAsAsync<Product>();
Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Quantity);
}
}
}
}
}
ilk kayıt Ben uzun süre çevrimiçi seyir ve bazı siteleri buldum ama gerçekten iyi bir örnek bulamadık edilmiştir. Birisi bana bir örnek verebilir mi veya bana doğru yönde işaret edebilir mi? IEnumerable'ı bir şekilde kullanır mıyım?
İşte benim Dinlendirici WebAPI sınıfının Neredeyse orada
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 RestfulWebAPIExample.Models;
namespace RestfulWebAPIExample.Controllers
{
public class ProductsController : ApiController
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET api/Products
public IQueryable<Product> GetProducts()
{
return db.Products;
}
// GET api/Products/5
[ResponseType(typeof(Product))]
public IHttpActionResult GetProduct(int id)
{
Product product = db.Products.Find(id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
// PUT api/Products/5
public IHttpActionResult PutProduct(int id, Product product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != product.ID)
{
return BadRequest();
}
db.Entry(product).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST api/Products
[ResponseType(typeof(Product))]
public IHttpActionResult PostProduct(Product product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Products.Add(product);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = product.ID }, product);
}
// DELETE api/Products/5
[ResponseType(typeof(Product))]
public IHttpActionResult DeleteProduct(int id)
{
Product product = db.Products.Find(id);
if (product == null)
{
return NotFound();
}
db.Products.Remove(product);
db.SaveChanges();
return Ok(product);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool ProductExists(int id)
{
return db.Products.Count(e => e.ID == id) > 0;
}
}
}
Web API sunucu kodunuz nerede? Kontrolörünüzü bize göster. Ayrıca ne tür bir sorun veya istisna karşılaştığınız da net değil, lütfen açıklığa kavuşturun. –
Üzgünüz, mesaj göndericiye eklendi. – dkostraa