我不知道如何在瀏覽器中查看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;
}
}
你試過 - http:// localhost:8269/api/products/getproducts? – Manoj
是的我試過了,出現錯誤('ObjectContent'1'類型未能序列化內容類型'application/xml; charset = utf-8'的響應正文)。 – Swapna
請編輯你的ajax方法,你必須設置ContentType :應用程序/ XML中的Ajax請求? – Manoj