混合网络API控制器和现场控制器 [英] Mix web api controllers and site controllers
问题描述
我fiddeling在MVC 4测试版的新WEP API,并增加了一些新的API控制器,以我现有的MVC的网站。问题是我无法命名的网络API控制器相同,我现有的控制器。现在我已经给了他们的名字,如ProductApiController,但不是很restlike。什么是对这些新的控制器namegiving一个很好的策略时,将它们添加到现有的MVC的网站?
问题是我无法命名的网络API控制器相同,我现有的控制器。
您可以有相同名称的API控制器,现有的控制器。只要把它们放在不同的命名空间,以使编译器高兴。
例如:
命名空间MyAppName.Controllers
{
公共ProductsController类:控制器
{
公众的ActionResult指数()
{
变种产品= productsRepository.GetProducts();
返回查看(产品);
}
}
}
和API的控制器:
命名空间MyAppName.Controllers.Api
{
公共ProductsController类:ApiController
{
公开的IEnumerable<产品>得到()
{
返回productsRepository.GetProducts();
}
}
}
,然后你有: /产品
和 / API /产品
分别与工作
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 existing controllers. For now I have given them names like ProductApiController but that is not very restlike. What is a good strategy for namegiving of these new controllers when adding them to an existing mvc site?
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.
这篇关于混合网络API控制器和现场控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!