搜索

查看: 3073|回复: 11

[ASP.NET] 使用HttpClient增删改查ASP.NET Web API服务

[复制链接]
发表于 2023-5-4 11:32:13 | 显示全部楼层 |阅读模式
Editor 2023-5-4 11:32:13 3073 11 看全部
本篇体验使用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 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 products = new List();
        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 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 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 GetProductsByCategory(string category)
        {
            return
                repository.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

20221017103811963.png

20221017103811963.png


在控制台程序下添加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();
                    Console.WriteLine("{0}\t{1}元\t{2}",product.Name, product.Price, product.Category);
                }
            }
        }
把控制台项目设置为启动项目。

20221017103811964.png

20221017103811964.png


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() {
    new MyCustomFormatter(),
    new JsonMediaTypeFormatter(),
    new XmlMediaTypeFormatter()
};
resp.Content.ReadAsAsync[I]>(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 products = await response.Content.ReadAsAsync[I]>();
                    foreach (var item in products)
                    {
                        Console.WriteLine("{0}\t{1}元\t{2}", item.Name, item.Price, item.Category);
                    }
                    
                }
            }
        }

20221017103811965.png

20221017103811965.png


使用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 products = await response.Content.ReadAsAsync[I]>();
                    foreach (var item in products)
                    {
                        Console.WriteLine("{0}\t{1}元\t{2}", item.Name, item.Price, item.Category);
                    }
                    
                }
            }
        }

20221017103811966.png

20221017103811966.png


使用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 POST
                var 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 products = await response.Content.ReadAsAsync[I]>();
                    foreach (var item in products)
                    {
                        Console.WriteLine("{0}\t{1}元\t{2}", item.Name, item.Price, item.Category);
                    }
                    
                }
            }
        }

20221017103811967.png

20221017103811967.png


使用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 POST
                var 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 products = await response.Content.ReadAsAsync[I]>();
                    foreach (var item in products)
                    {
                        Console.WriteLine("{0}\t{1}元\t{2}", item.Name, item.Price, item.Category);
                    }
                    
                }
            }
        }

20221017103811968.png

20221017103811968.png


以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对知鸟论坛的支持。如果你想了解更多相关内容请查看下面相关链接
回复

使用道具 举报

发表于 2023-6-28 19:13:09 | 显示全部楼层
麻辣鸡翅 2023-6-28 19:13:09 看全部
楼主,我太崇拜你了!我想我是一天也不能离开知鸟论坛
回复

使用道具 举报

发表于 2023-6-29 13:40:30 | 显示全部楼层
塞翁364 2023-6-29 13:40:30 看全部
感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
回复

使用道具 举报

发表于 2023-6-29 15:02:12 | 显示全部楼层
123456819 2023-6-29 15:02:12 看全部
这个帖子不回对不起自己!我想我是一天也不能离开知鸟论坛
回复

使用道具 举报

发表于 2023-6-29 18:00:59 | 显示全部楼层
普通人物怨 2023-6-29 18:00:59 看全部
这东西我收了!谢谢楼主!知鸟论坛真好!
回复

使用道具 举报

发表于 2023-6-29 20:03:13 | 显示全部楼层
当当当当裤裆坦 2023-6-29 20:03:13 看全部
楼主发贴辛苦了,谢谢楼主分享!我觉得知鸟论坛是注册对了!
回复

使用道具 举报

发表于 2023-6-29 22:20:45 | 显示全部楼层
落败的青春阳落s 2023-6-29 22:20:45 看全部
论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
回复

使用道具 举报

发表于 2023-6-30 09:18:18 | 显示全部楼层
井底燕雀傥 2023-6-30 09:18:18 看全部
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
回复

使用道具 举报

发表于 2023-6-30 10:56:07 | 显示全部楼层
冀苍鸾 2023-6-30 10:56:07 看全部
论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
回复

使用道具 举报

发表于 2023-6-30 15:50:22 | 显示全部楼层
墙和鸡蛋 2023-6-30 15:50:22 看全部
论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
回复

使用道具 举报

  • 您可能感兴趣
点击右侧快捷回复 【请勿灌水】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 返回列表

RSS订阅| SiteMap| 小黑屋| 知鸟论坛
联系邮箱E-mail:zniao@foxmail.com
快速回复 返回顶部 返回列表