Today i have faced a very stupid problem, i was working something in ASP.NET MVC 4 Web API...First of all i created a new ASP.NET MVC 4 Project and select WEB API. Now as you can see there are two controllers has been generated by default in controller folder "HomeController" and "ValuesController", i created a model class and and write code in valuescontroller and access like this.
http://localhost:49012/api/values
That works perfect.
Now i created 1 more model class:
Product:
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
Now i need to add a new controller, so as usual i added new controller class in controller folder name is "ProductController" and write this code.
public class ProductController: Controller
{
//
// GET: /Default1/
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;
}
}
Now from my side everything is fine, so i try to access my web api like this.
http://localhost:49012/api/product
This given me this error message:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error><Message>No HTTP resource was found that matches the request URI 'http://localhost:49012/api/product'.</Message><MessageDetail>No type was found that matches the controller named 'yogesh'.</MessageDetail></Error>
Now i searched on Google but did't get any solution, so suddenly i have seen default controller.
public class ProductController: Controller
Now i realized my controller class is inhering controller but default controller is inheriting ApiController like this under namespace of
using System.Web.Http;
public class ProductController: ApiController
That resolved my problem.