ASP.. NET Web API没有得到新发布的项目

本文关键字:新发布 项目 NET Web API ASP | 更新日期: 2023-09-27 18:08:44

我正在学习如何使用ASP。. NET Web API工作,因为我需要它的一个项目,我正在工作。我使用OWIN在WPF应用程序中托管Web API。

我有麻烦让我的POST方法工作。当我发布一个新产品时,它被添加到产品列表中,但当我试图获取所有产品时,它没有显示。

这是一个我用来测试的类:

class Program
    {
        const string baseAddress = "http://localhost:9000/";
        static void Main(string[] args)
        {

            // Start OWIN host 
            using (WebApp.Start<Startup>(url: baseAddress))
            {
                RunAsync().Wait();
            }
            Console.ReadLine();
        }
        static async Task RunAsync()
        {
            using(var client = new HttpClient())
            {
                client.BaseAddress = new Uri(baseAddress);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //GET initial products
                HttpResponseMessage response = await client.GetAsync("api/products/");
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine(await response.Content.ReadAsStringAsync() + "'n");
                }
                //POST new product
                Product gizmo = new Product() { Id = 4, Name = "Gizmo", Price = 50, Category = "Widget"};
                try
                {
                    response = await client.PostAsJsonAsync("api/products", gizmo);
                    response.EnsureSuccessStatusCode();
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine(e.Message);
                }
                //GET all products (should contain gizmo product)
                response = await client.GetAsync("api/products/");
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine(await response.Content.ReadAsStringAsync() + "'n");
                }
            }
        }
    }

这是我的控制器类:

public class ProductsController : ApiController
    {
        List<Product> products = new List<Product>();
        public ProductsController()
        {
            products.Add(new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 });
            products.Add(new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M });
            products.Add(new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M });
        }
        public IEnumerable<Product> Get()
        {
            return products;
        }
        public IHttpActionResult Get(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);
            if (product == null)
            {
                return NotFound();
            }
            return Ok(product);
        }
        public void Post(Product product) {
            products.Add(product);
            Console.WriteLine("Product Added: " + products[products.Count - 1].Name + "'n");
        }
    }

输出显示新的Gizmo项目被添加到列表中:

[{"Id":1,"Name":"Tomato Soup","Category":"Groceries","Price":1.0},{"Id":2,"Name":"Yo-yo","Category":"Toys","Price":3.75},{"Id":3,"Name":"Hammer","Category":"Hardware","Price":16.99}]
Product Added: Gizmo
[{"Id":1,"Name":"Tomato Soup","Category":"Groceries","Price":1.0},{"Id":2,"Name":"Yo-yo","Category":"Toys","Price":3.75},{"Id":3,"Name":"Hammer","Category":"Hardware","Price":16.99}]

ASP.. NET Web API没有得到新发布的项目

你的控制器构造函数可能会被每个请求调用(记住,HTTP是无状态的)。

List<Product>使用持久存储