2 csdn847580 csdn847580 于 2016.04.22 15:42 提问

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显示无法显示

1个回答

caozhy
caozhy   Ds   Rxr 2016.04.23 06:34

默认webapi只允许post,你需要加上[HttpGet]在GetAllProducts上

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!