问题描述
- WebApi运行浏览地址,总是无法显示网页
- 从网上找的WebApi的示例,就是实体类加控制器 Product[] products = new Product[]
{
new Product { Id = 1 Name = ""Tomato Soup"" Category = ""Groceries"" Price = 1 }
new Product { Id = 2 Name = ""Yo-yo"" Category = ""Toys"" Price = 3.75M }
new Product { Id = 3 Name = ""Hammer"" Category = ""Hardware"" Price = 16.99M }
};public IEnumerable<Product> GetAllProducts() { return products; } 路由也配好了 public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: ""DefaultApi"" routeTemplate: ""api/{controller}/{id}"" defaults: new { id = RouteParameter.Optional } );
为什么从浏览器浏览api/products显示无法显示
解决方案
默认webapi只允许post,你需要加上[HttpGet]在GetAllProducts上
时间: 2024-10-30 08:03:16