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

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

问题描述

我创建一个新的项目MVC4,最近阅读本文中有关网络API:<一href=\"http://encosia.com/asp-net-web-api-vs-asp-net-mvc-apis/\">http://encosia.com/asp-net-web-api-vs-asp-net-mvc-apis/

I am creating a new MVC4 project and have recently read this article regarding web API: http://encosia.com/asp-net-web-api-vs-asp-net-mvc-apis/

这使我进一步从我发现研究,从JavaScript传递给服务器端通过更好的Web API框架,而不是控制器动作现在已经实现。我的理解是正确的吗?

This lead me to research further and from I have found, 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?

我是presuming,我可以分享我的Web API和MVC控制器之间的所有属性等等等等面对它,它似乎不是我的巨大变化。

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.

当我设置应用程序,我喜欢组件出到项目的分配。我的计划是有一个MVC项目和一个Web API项目。但我到问题已经跑了。例如,我已经结束了2个应用程序本身,独立的路由设置等等等等。

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.

所以我的问题是,在一个MVC应用程序应该在Web API框架坐在同一个项目中,还是应该在Web API被分离成它自己的项目和工作周围的问题是什么?

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?

推荐答案

不幸的是你错了 - 我是presuming,我可以分享我的Web API和MVC控制器等之间的所有属性等的面对它,它似乎不是一个巨大变化给我。

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.

许多被Web API和MVC使用的概念,尽管乍一看差不多,实际上是不兼容的。例如,Web API的属性是 System.Web.Http.Filters.Filter 和MVC属性是 System.Web.Mvc.Filter - 他们是不能互换的。

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.

同样适用于许多其它的概念 - 模型绑定(完全不同的机制),路线(网页API使用HTTPRoutes不路线,即使它们都在相同的基础RouteTable操作),依赖解析器(不兼容)和多 - 即使表面上的相似,在实践中是非常不同的。此外,网页API不具有区域概念

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.

最后,如果你正在尝试做的实现是有一个新的,时尚的服务了JSON内容的方式 - 前都要沿着这条道路三思而后行。我当然不会建议任何重构现有code,除非你真的寻找到拥抱HTTP和构建您的应用程序REST方式。

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.

这一切真的取决于你正在构建的内容。如果你开始一个新的项目,以及所有你需要的是服务于一些JSON,以方便您的Web应用程序 - 只要你愿意忍受一些可能重复code(就像我上面提到的东西),网页API可以很容易地同一项目的ASP.NET MVC内举行。

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.

我只会单独的Web API到一个单独的项目,如果你要建立一个适当的API为您的在线服务 - 可能由外部客户消费,或者通过各种设备 - 比如你加油的移动应用程序。

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天全站免登陆