Mix web api controllers and site controllers

前端 未结 1 456
礼貌的吻别
礼貌的吻别 2021-01-31 08:41

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

1条回答
  •  时光说笑
    2021-01-31 09:22

    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 Get()
            {
                return productsRepository.GetProducts();
            }
        }
    }
    

    and then you have: /products and /api/products respectively to work with.

    0 讨论(0)
提交回复
热议问题