2016-12-07 69 views
-1

我不知道如何在浏览器中查看get api结果。我试图让这个网址(http://localhost:8269/api/getproducts)的响应,但得到一个错误:无法获取webapi结果

<Error> 
    <Message> 
     No HTTP resource was found that matches the request 
     URI 'http://localhost:8269/api/getProducts'. 
    </Message> 
    <MessageDetail> 
     No type was found that matches the controller named 'getProducts'. 
    </MessageDetail> 
</Error> 
public class ProductsController : ApiController 
{ 
    Product[] products = new Product[] 
    { 
     new Product { ProductId=1,ProductName="samsung",ProductCategory="mobile",ProductPrice=7889 }, 
     new Product { ProductId=1,ProductName="nokia",ProductCategory="mobile",ProductPrice=7844 }, 
     new Product { ProductId=1,ProductName="lg",ProductCategory="mobile",ProductPrice=7887 }, 
     new Product { ProductId=1,ProductName="xiomi",ProductCategory="mobile",ProductPrice=7856 }, 
     new Product { ProductId=1,ProductName="htc",ProductCategory="mobile",ProductPrice=7833 } 
    }; 

    public IEnumerable getProducts() 
    { 
     return products; 
    } 
} 
+0

你试过 - http:// localhost:8269/api/products/getproducts? – Manoj

+0

是的我试过了,出现错误('ObjectContent'1'类型未能序列化内容类型'application/xml; charset = utf-8'的响应正文)。 – Swapna

+0

请编辑你的ajax方法,你必须设置ContentType :应用程序/ XML中的Ajax请求? – Manoj

回答

3

你的URI应该是'http://localhost:8269/api/Products'(使用默认路由是...)

当您使用的是Web API 2,您的示例应该可以正常使用上述网址。 使用Web API 1时,虽然只需调用Get()或使用[HttpGet]属性修饰您的方法。

[HttpGet] 
public IEnumerable getProducts() 
{ 
    return products; 
} 

我建议你看看asp.net web apithis answer基本有点接近。

+0

还值得关注其他资源命名的基础 - http://www.restapitutorial.com/lessons/restfulresourcenaming.html –