国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

使用HttpClient增刪改查ASP.NET Web API服務

瀏覽:97日期:2022-06-08 15:57:54

本篇體驗使用HttpClient對ASP.NET Web API服務實現增刪改查。

創建ASP.NET Web API項目

新建項目,選擇"ASP.NET MVC 4 Web應用程序"。

選擇"Web API"。

在Models文件夾下創建Product類。

    public class Product    {public int Id { get; set; }public string Name { get; set; }public string Category { get; set; }public decimal Price { get; set; }    }

在Models文件夾下創建IProductRepository接口。

    public interface IProductRepository    {IEnumerable<Product> GetAll();Product Get(int id);Product Add(Product item);void Remove(int id);bool Update(Product item);    }

在Models文件夾下創建ProductRepository類,實現IProductRepository接口。

   public class ProductRepository : IProductRepository    {private List<Product> products = new List<Product>();private int _nextId = 1;public ProductRepository(){    Add(new Product() {Name = "product1", Category = "sports", Price = 88M});    Add(new Product() { Name = "product2", Category = "sports", Price = 98M });    Add(new Product() { Name = "product3", Category = "toys", Price = 58M });}public IEnumerable<Product> GetAll(){    return products;}public Product Get(int id){    return products.Find(p => p.Id == id);}public Product Add(Product item){    if (item == null)    {throw new ArgumentNullException("item");    }    item.Id = _nextId++;    products.Add(item);    return item;}public bool Update(Product item){    if (item == null)    {throw new ArgumentNullException("item");    }    int index = products.FindIndex(p => p.Id == item.Id);    if (index == -1)    {return false;    }    products.RemoveAt(index);    products.Add(item);    return true;}public void Remove(int id){    products.RemoveAll(p => p.Id == id);}    }

在Controllers文件夾下創建空的ProductController。

   public class ProductController : ApiController    {static readonly IProductRepository repository = new ProductRepository();//獲取所有public IEnumerable<Product> GetAllProducts(){    return repository.GetAll();}//根據id獲取public Product GetProduct(int id){    Product item = repository.Get(id);    if (item == null)    {throw new HttpResponseException(HttpStatusCode.NotFound);    }    return item;}//根據類別查找所有產品public IEnumerable<Product> GetProductsByCategory(string category){    returnrepository.GetAll().Where(p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));}//創建產品public HttpResponseMessage PostProduct(Product item){    item = repository.Add(item);    var response = Request.CreateResponse(HttpStatusCode.Created, item);    string uri = Url.Link("DefaultApi", new {id = item.Id});    response.Headers.Location = new Uri(uri);    return response;}//更新產品public void PutProduct(int id, Product product){    product.Id = id;    if (!repository.Update(product))    {throw new HttpResponseException(HttpStatusCode.NotFound);    }}//刪除產品public void DeleteProduct(int id){    Product item = repository.Get(id);    if (item == null)    {throw new HttpResponseException(HttpStatusCode.NotFound);    }    repository.Remove(id);}    }

在瀏覽器中輸入:

http://localhost:1310/api/Product 獲取到所有產品
http://localhost:1310/api/Product/1 獲取編號為1的產品

使用HttpClient查詢某個產品

在同一個解決方案下創建一個控制臺程序。

依次點擊"工具","庫程序包管理器","程序包管理器控制臺",輸入如下:

Install-Package Microsoft.AspNet.WebApi.Client

在控制臺程序下添加Product類,與ASP.NET Web API中的對應。

    public class Product    {public string Name { get; set; }public double Price { get; set; }public string Category { get; set; }     }

編寫如下:

static void Main(string[] args){    RunAsync().Wait();    Console.ReadKey();}static async Task RunAsync(){    using (var client = new HttpClient())    {//設置client.BaseAddress = new Uri("http://localhost:1310/");client.DefaultRequestHeaders.Accept.Clear();client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//異步獲取數據HttpResponseMessage response = await client.GetAsync("/api/Product/1");if (response.IsSuccessStatusCode){    Product product = await response.Content.ReadAsAsync<Product>();    Console.WriteLine("{0}\t{1}元\t{2}",product.Name, product.Price, product.Category);}    }}

把控制臺項目設置為啟動項目。

HttpResponseMessage的IsSuccessStatusCode只能返回true或false,如果想讓響應拋出異常,需要使用EnsureSuccessStatusCode方法。

try{    HttpResponseMessage response = await client.GetAsync("/api/Product/1");    response.EnsureSuccessStatusCode();//此方法確保響應失敗拋出異常}catch(HttpRequestException ex){    //處理異常}

另外,ReadAsAsync方法,默認接收MediaTypeFormatter類型的參數,支持 JSON, XML, 和Form-url-encoded格式,如果想自定義MediaTypeFormatter格式,參照如下:

var formatters = new List<MediaTypeFormatter>() {    new MyCustomFormatter(),    new JsonMediaTypeFormatter(),    new XmlMediaTypeFormatter()};resp.Content.ReadAsAsync<IEnumerable<Product>>(formatters);

使用HttpClient查詢所有產品

       static void Main(string[] args){    RunAsync().Wait();    Console.ReadKey();}static async Task RunAsync(){    using (var client = new HttpClient())    {//設置client.BaseAddress = new Uri("http://localhost:1310/");client.DefaultRequestHeaders.Accept.Clear();client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//異步獲取數據HttpResponseMessage response = await client.GetAsync("/api/Product");if (response.IsSuccessStatusCode){    IEnumerable<Product> products = await response.Content.ReadAsAsync<IEnumerable<Product>>();    foreach (var item in products)    {Console.WriteLine("{0}\t{1}元\t{2}", item.Name, item.Price, item.Category);    }    }    }}

使用HttpClient添加

       static void Main(string[] args){    RunAsync().Wait();    Console.ReadKey();}static async Task RunAsync(){    using (var client = new HttpClient())    {//設置client.BaseAddress = new Uri("http://localhost:1310/");client.DefaultRequestHeaders.Accept.Clear();client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//添加var myProduct = new Product() { Name = "myproduct", Price = 88, Category = "other" };HttpResponseMessage response = await client.PostAsJsonAsync("api/Product", myProduct);//異步獲取數據response = await client.GetAsync("/api/Product");if (response.IsSuccessStatusCode){    IEnumerable<Product> products = await response.Content.ReadAsAsync<IEnumerable<Product>>();    foreach (var item in products)    {Console.WriteLine("{0}\t{1}元\t{2}", item.Name, item.Price, item.Category);    }    }    }}

使用HttpClient修改

       static void Main(string[] args){    RunAsync().Wait();    Console.ReadKey();}static async Task RunAsync(){    using (var client = new HttpClient())    {//設置client.BaseAddress = new Uri("http://localhost:1310/");client.DefaultRequestHeaders.Accept.Clear();client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//添加 HTTP POSTvar myProduct = new Product() { Name = "myproduct", Price = 100, Category = "other" };HttpResponseMessage response = await client.PostAsJsonAsync("api/product", myProduct);if (response.IsSuccessStatusCode){    Uri pUrl = response.Headers.Location;    //修改 HTTP PUT    myProduct.Price = 80;   // Update price    response = await client.PutAsJsonAsync(pUrl, myProduct);}//異步獲取數據response = await client.GetAsync("/api/Product");if (response.IsSuccessStatusCode){    IEnumerable<Product> products = await response.Content.ReadAsAsync<IEnumerable<Product>>();    foreach (var item in products)    {Console.WriteLine("{0}\t{1}元\t{2}", item.Name, item.Price, item.Category);    }    }    }}

使用HttpClient刪除

static void Main(string[] args){    RunAsync().Wait();    Console.ReadKey();}static async Task RunAsync(){    using (var client = new HttpClient())    {//設置client.BaseAddress = new Uri("http://localhost:1310/");client.DefaultRequestHeaders.Accept.Clear();client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//添加 HTTP POSTvar myProduct = new Product() { Name = "myproduct", Price = 100, Category = "other" };HttpResponseMessage response = await client.PostAsJsonAsync("api/product", myProduct);if (response.IsSuccessStatusCode){    Uri pUrl = response.Headers.Location;    //修改 HTTP PUT    myProduct.Price = 80;   // Update price    response = await client.PutAsJsonAsync(pUrl, myProduct);    //刪除 HTTP DELETE    response = await client.DeleteAsync(pUrl);}//異步獲取數據response = await client.GetAsync("/api/Product");if (response.IsSuccessStatusCode){    IEnumerable<Product> products = await response.Content.ReadAsAsync<IEnumerable<Product>>();    foreach (var item in products)    {Console.WriteLine("{0}\t{1}元\t{2}", item.Name, item.Price, item.Category);    }    }    }}

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對的支持。如果你想了解更多相關內容請查看下面相關鏈接

標簽: ASP.NET
相關文章:
主站蜘蛛池模板: 亚洲男人的天堂视频 | 在线视频一二三区 | 特黄特a级特别特级特毛片 特黄特黄 | 欧美自拍视频在线 | 精品欧美一区二区三区免费观看 | 欧美成人免费网在线观看 | 韩国毛片免费看 | 日韩美女免费视频 | 中文字幕亚洲综合久久男男 | 国产九九视频在线观看 | 久热精品6 | 99国产在线视频 | 日韩欧美一级毛片视频免费 | 色婷婷激婷婷深爱五月老司机 | 自拍网在线 | 亚洲免费成人在线 | 国产欧美综合一区二区 | 免费一级淫片aaa片毛片a级 | 一级毛片在线视频 | 91香蕉嫩草 | 中文字幕精品在线观看 | 日本三级韩国三级在线观看a级 | 久草视频在线播放 | 国产精品单位女同事在线 | 国产日产欧美a级毛片 | 国产精品特级毛片一区二区三区 | 亚洲综合色视频在线观看 | 丝袜紧身裙国产在线播放 | 久精品在线观看 | 婷婷色综合久久五月亚洲 | 亚洲图片一区二区 | 老司机午夜性生免费福利 | 亚洲成a v人片在线看片 | 绝对真实偷拍盗摄高清在线视频 | 国内交换一区二区三区 | 成人性一级视频在线观看 | 欧美巨大精品欧美一区二区 | 国产成人毛片精品不卡在线 | 国产午夜免费视频片夜色 | 亚洲精品国自产拍在线观看 | 欧美国产综合日韩一区二区 |