I am fiddeling with the new wep api in mvc 4 beta and adding some new api controllers to my existing mvc site. Problem is I can\'t name the web api controllers the same as my ex
Problem is I can't name the web api controllers the same as my existing controllers.
You could have your API controllers with the same name as your existing controllers. Just put them in a different namespace to make the compiler happy.
Example:
namespace MyAppName.Controllers
{
public class ProductsController: Controller
{
public ActionResult Index()
{
var products = productsRepository.GetProducts();
return View(products);
}
}
}
and the API controller:
namespace MyAppName.Controllers.Api
{
public class ProductsController: ApiController
{
public IEnumerable<Product> Get()
{
return productsRepository.GetProducts();
}
}
}
and then you have: /products
and /api/products
respectively to work with.