混合网络API控制器和现场控制器 [英] Mix web api controllers and site controllers

查看:139
本文介绍了混合网络API控制器和现场控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆