我可以同时拥有一个控制器和一个ApiController同样的事情? [英] Can I have both a Controller and an ApiController for the same thing?

查看:151
本文介绍了我可以同时拥有一个控制器和一个ApiController同样的事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用VS 2012 RC,而我创造,我打算同时提供基于HTML的用户界面和的WebAPI的编程接口的ASP.NET MVC 4 Web应用程序。

I've just started to use the VS 2012 RC, and I'm creating an ASP.NET MVC 4 web application in which I plan to provide both an HTML-based user interface and a WebApi-based programming interface.

有关我的HTML网页,我有一个控制器和视图,我的每个模型(MVC!),以及路由的作品按照惯例,这样,例如,URL /客户端挂钩到了我的 ClientController 。我的 ClientController 导出控制器

For my HTML website, I have a controller and view for each of my models (MVC!), and the routing works "by convention" so that, for example, the URL /client hooks up to my ClientController. My ClientController derives from Controller.

有关我的API,我将创建一个由 ApiController 派生出新的控制器。我当然希望我的API的网址是类似我的HTML的网址,所以我想在客户端信息可用在 / API /客户端。然而,随着由会展路由,这将意味着,我需要一个名为 ClientController 的ApiController。而且我已经有一个 ClientController 类。

For my API, I will create new controllers that derive from ApiController. I naturally want my API URLs to be similar to my HTML URLs, so I'd like the client info to be available at /api/client. However, with the by-convention routing, that would suggest that I need an ApiController named ClientController. And I already have a ClientController class.

我该如何面对呢?我是否需要自定义路由?难道我把API类在不同的命名空间,这样我可以给他们相同的名字?

How do I deal with this? Do I need custom routing? Do I put the API classes in different namespace so that I can give them the same name?

更新:这个问题似乎表明,不同的命名空间为我的API控制器是我所需要的:的混合网络API控制器和现场控制器

Update: this question seems to suggest that a different namespace for my API controllers is all I need: Mix web api controllers and site controllers

推荐答案

它所需要的是控制器类是在一个不同的命名空间,一切都很好。

All it requires is for the controller classes to be in a different namespace, and all is well.

使用MVC区也将工作(如在gordonml的评论所建议的),但是这有效地使在不同的命名空间的控制器,所以它的实现相同的结果的更正式的方式

Using MVC areas would also work (as suggested in gordonml's comment), but this effectively puts the controllers in different namespaces, so it's a more formal way of achieving the same result.

这篇关于我可以同时拥有一个控制器和一个ApiController同样的事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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