什么是Web API和ASP MVC之间的主要差异 [英] What are the major differences between Web API and ASP MVC

查看:89
本文介绍了什么是Web API和ASP MVC之间的主要差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题确实总结了我的问题。我已经使用这两种技术,但我不确定的是人们提供了比其它基本不同。从本质上说:

The title really sums up my question. I have used both technologies but I am uncertain as to what one offers that is substantially different than the other. In essence:

什么样的​​标准和/或指导应选择ASP MVC或Web API时,设计一个基于REST的Web应用程序,当一个考虑?

What criteria and/or guidance should one consider when selecting ASP MVC or Web API when designing a restful web application?

推荐答案

目的

ASP.NET MVC专注于让HTML输出容易。的ASP.NET Web API专注于制造原始数据的输出容易。

ASP.NET MVC is focused on making output of HTML easy. ASP.NET Web API is focused on making output of raw data easy.

在世界的WebForms,ASP.NET MVC将相当于.aspx页和ASP.NET Web API会的.asmx。

In the WebForms world, ASP.NET MVC would be equivalent to .aspx pages and ASP.NET Web API would be .asmx.

虽然没有什么是不可能

虽然它可能使Web API输出HTML和MVC输出的原始数据,您将需要额外工作。例如,使得其他类中的Web API内容协商,或添加逻辑来处理在MVC的OData查询过程中处理文本/ HTML。

While it's possible to make Web API output HTML and MVC output raw data, you will need additional work. For example, making additional classes to handle text/html during content negotiation in Web API, or adding logic to handle OData queries in MVC.

假设

为MVC和Web API默认的假设也不同。 MVC默认情况下,假定用户提交的数据可以来自多个来源,无论是在查询字符串或形式。

The default assumption for both MVC and Web API are different too. MVC by default assumes user submitted data can come from multiple sources, be it in the query string or in the form.

默认的Web API假定基本类型来自查询字符串和非基本类型来自于形式。它还假定你只想要阅读的形式身体一次,没有缓存,具有较低的内存使用量和更好的性能。

Web API by default assumes that primitive type comes from query string and non-primitive types comes from the form. It also assumes that you would only want to read the form body once, without caching, to have lower memory usage and better performance.

逆着默认设置需要额外的工作,这对我来说,没有任何意义的。

Going against the defaults requires additional work, that to me, does not make sense at all.

编辑:

此外,GET AJAX请求被阻止MVC的JsonResult默认为prevent CSRF,而Web API允许GET默认情况下AJAX请求。

Also, GET AJAX request is blocked by MVC's JsonResult by default to prevent CSRF, while Web API allows GET AJAX request by default.

更新MVC 6

6 MVC MVC统一和Web API,并允许您在MVC像返回的ViewResult或对象就像在网络API和框架会照顾内容协商,创建HTML,JSON或XML为您服务。更低的内存使用率也来到MVC中,通过使用它的自定义管道,而不是什么是的System.Web提供。

MVC 6 unifies MVC and Web API and allows you to return ViewResult like in MVC or object like in Web API, and the framework will take care of content negotiation, creating the HTML, JSON or XML for you. Lower memory usage also arrives in MVC, by using its custom pipeline instead of what is provided by System.Web.

所以前进着,还有的MVC和Web API之间没有区别。

So going forward, there's no distinction between the MVC and Web API.

这篇关于什么是Web API和ASP MVC之间的主要差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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