单独项目中MVC解决方案中的Web API [英] Web API in MVC solution in separate project

查看:32
本文介绍了单独项目中MVC解决方案中的Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新的 MVC4 项目,研究使我相信现在通过 Web API 框架而不是控制器操作可以更好地实现从 javascript 到服务器端的通信.我的理解是否正确?

我假设我可以在 Web API 和 MVC 控制器之间共享我的所有属性等,所以从表面上看,这对我来说似乎没有太大的变化.

在设置应用程序时,我喜欢将组件拆分为项目.我的计划是有一个 MVC 项目和一个 Web API 项目.但我遇到了问题.例如,我最终得到了 2 个应用程序,单独的路由设置等.

所以我的问题是,在 MVC 应用程序中,Web API 框架应该位于同一个项目中,还是应该将 Web API 分成自己的项目并解决这些问题?

解决方案

不幸的是你错了 - 我假设我可以在 web api 和 mvc 控制器之间共享我的所有属性等,所以从表面上看,这对我来说似乎没有太大的变化.

Web API 和 MVC 使用的许多概念,尽管乍一看很相似,但实际上并不兼容.例如,Web API 属性为 System.Web.Http.Filters.Filter,MVC 属性为 System.Web.Mvc.Filter - 它们不可互换.>

同样适用于许多其他概念 - 模型绑定(完全不同的机制)、路由(Web API 使用 HTTPRoutes 而不是路由,即使它们都在相同的底层 RouteTable 上运行)、依赖解析器(不兼容)等等 - 即使表面上相似,实际却大相径庭.而且,Web API 没有区域的概念.

最终,如果您想要实现的只是拥有一种新的、时尚的"方式来提供 JSON 内容 - 在走这条路之前请三思.我当然不建议重构任何现有代码,除非您真的正在考虑采用 HTTP 并以 RESTful 方式构建您的应用程序.

这完全取决于您正在构建的内容.如果您正在开始一个新项目,并且您所需要的只是提供一些 JSON 以促进您的 Web 应用程序 - 如果您愿意接受一些可能重复的代码(如我上面提到的东西),Web API 可以轻松地托管在其中与 ASP.NET MVC 相同的项目.

如果您要为您的在线服务构建合适的 API,我只会将 Web API 分离到一个单独的项目中 - 可能由外部客户或各种设备使用 - 例如为您的移动应用程序提供动力.

I am creating a new MVC4 project, and research has lead me to believe that communicating from javascript to the server side is better achieved now through web API framework rather than controller actions. Is my understanding correct on this?

I am presuming that I can share all my attributes etc between web API and MVC controllers so on the face it, it does not seem a massive change for me.

When I am setting up applications, I like to split components out in to projects. My plan was to have a MVC project and a web API project. But I have ran in to issues. For example I have ended up with 2 apps as such, separate routing set up etc etc.

So my question is, in a MVC application should the web API framework sit within the same project, or should the web API be separated into a project of its own and work around the issues?

解决方案

Unfortunately you are wrong about that - I am presuming that I can share all my attributes etc between web api and mvc controllers so on the face it, it does not seem a massive change for me.

Many of the concepts used by Web API and MVC, even though similar at first glance, are actually not compatible. For example, Web API attributes are System.Web.Http.Filters.Filter and MVC attributes are System.Web.Mvc.Filter - and they are not interchangeable.

Same applies to many other concepts - model binding (completely different mechanisms), routes (Web API uses HTTPRoutes not Routes, even though they both operate on the same underlying RouteTable), dependency resolver (not compatible) and more - even though similar on the surface, are very different in practice. Moreover, Web API does not have a concept of areas.

Ultimately, if all you are trying to do achieve is to have a "new, trendy" way of serving up JSON content - think twice before going down that path. I certainly wouldn't recommend refactoring any existing code unless you are really looking into embracing HTTP and building your app in a RESTful way.

It all really depends on what you are building. If you are starting a new project, and all you need is to serve up some JSON to facilitate your web app - provided you willing to live with some potentially duplicate code (like the stuff I mentioned above), Web API could easily be hosted within the same project as ASP.NET MVC.

I would only separate Web API into a separate project if you are going to build a proper API for your online service - perhaps to be consumed by external customers, or by various devices - such as fueling your mobile apps.

这篇关于单独项目中MVC解决方案中的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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