混合网页API和ASP.Net MVC的页面在一个项目 [英] Mixing Web Api and ASP.Net MVC Pages in One Project
问题描述
我如何在一个项目中混合的Web API和ASP.Net MVC页?
How do I mix Web API and ASP.Net MVC pages in one project?
比如说,我有模式的用户。我想,在同一个项目中,有将所有的HTTP动词响应,用于管理用户的实体,并在同一时间有一个控制器,将返回根据要求采取行动的适当强类型的意见ApiController。
For instance, I have model User. I would like, within the same project, to have an ApiController that would respond to all the HTTP verbs for managing the User entities, and at the same time have a Controller that would return the appropriate strongly-typed Views depending on the Action requested.
我不能说出两个控制器UserController的。什么是解决这个问题的最好方法?我应该指定一名UserApiController和其他UserController的?任何其他建议?
I can't name both controllers UserController. What is the best way around this? Should I name one UserApiController and the other UserController? Any other suggestions?
推荐答案
您可以把它们放在不同的命名空间,例如MyApp.Controllers.UsersController和MyApp.Controllers.WebAPI.UsersController。
You can put them in separate namespaces, e.g MyApp.Controllers.UsersController and MyApp.Controllers.WebAPI.UsersController.
这会让你暴露了MVC和的WebAPI类似的URI路径,例如:
This would let you expose similar URI routes in MVC and WebAPI, eg:
/users/1 << MVC view
/api/users/1 << Web API
这篇关于混合网页API和ASP.Net MVC的页面在一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!