在一个项目中混合 Web 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?
例如,我有模型用户.我希望在同一个项目中,有一个 ApiController 来响应所有用于管理用户实体的 HTTP 动词,同时有一个控制器,它会根据请求的操作返回适当的强类型视图.
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
这篇关于在一个项目中混合 Web Api 和 ASP.Net MVC 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!