使用的WebAPI或MVC在ASP.NET中返回JSON [英] Using WebAPI or MVC to return JSON in ASP.NET

查看:297
本文介绍了使用的WebAPI或MVC在ASP.NET中返回JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个ASP.NET MVC应用程序,客户端脚本重,它将使用JSON和jQuery操作DOM。

I'm building an ASP.NET MVC application that is client-script heavy, it will use JSON and jQuery to manipulate the DOM.

我的理解是既网络API控制器 MVC控制器可以返回JSON。

My understanding is both Web API Controller and MVC Controller can return JSON.

由于我的方案,我应该使用网络API控制器 MVC控制器

Given my scenario, should I use a Web API Controller or an MVC Controller?

推荐答案

网络可以创建在任何ASP.NET应用程序中,不只是MVC应用托管API控制器。因此,一个显而易见的原因,以创建一个Web API是,如果你没有一个MVC前端(如经典之作,由贵公司/机构托管的RESTful网络服务)。

Web API Controllers can be created and hosted in any ASP.NET Application, not just MVC applications. Thus, an obvious reason to create a Web API is if you do not have an MVC front-end (e.g. classic, RESTful web-services hosted by your company/organization.)

MVC控制器通常依靠MVC框架,如果你看一下默认的模板和最受社会和你的同龄人所做的工作,你会发现,几乎所有的MVC控制器与心目中的视图实现。

MVC Controllers typically rely on the MVC Framework, if you look at default templates and most of the work done by the community and your peers you will notice that almost all MVC Controllers are implemented with the View in mind.

就个人而言,我用MVC控制器,当我打算用视图()回应,我会使用Web API的任何东西,不依赖于特定视图。

Personally, I use MVC Controllers when I intend to respond with a View(), and I'll use a Web API for anything that isn't dependent on a particular view.

有注意事项,当然,但一般来说,如果你不需要MVC的模型绑定的行为,你的服务是以数据为中心,和操作数据为中心的(​​例如CRUD操作),那么你可能需要一个'网页API控制器,而不是模型 - 视图 - 控制器。相反,如果你的操作视图为中心的(​​如提供一个用户管理页面给用户),或者你需要MVC的模型绑定,生成阿贾克斯的谐音(不太可能),那么你会希望有一个MVC控制器来代替。

There are caveats, of course, but generally speaking if you don't require the Model Binding behavior of MVC, your service is data-centric, and operations are Data-centric (e.g. CRUD operations) then you likely want a 'Web API Controller' instead of a 'Model-View Controller'. Conversely, if your operations are View-centric (e.g. delivering a user admin page to the user), or you need MVC's Model Binding to generate 'ajax partials' (very unlikely), then you will want an MVC Controller instead.

就个人而言,我使用的Web API控制器驱动基于JSON的REST风格的客户,我用MVC控制器来处理的SPA基本的浏览器路由和传递。

Personally, I use Web API controllers for driving JSON-based RESTful clients, I use MVC controllers for handling basic browser routing and delivery of the SPA.

这篇关于使用的WebAPI或MVC在ASP.NET中返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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