ASP.NET MVC和ASP.NET Web API区别(Web API初级教程二)(ASP. Net MVC and ASP Net web API differences (WEB API tutorial II))

原文链接:https://www.dotnettricks.com/learn/webapi/difference-between-aspnet-mvc-and-aspnet-web-api

自从MVC框架可以通过使用JsonResult返回JSON数据,还可以处理简单的AJAX请求。许多开发人员在使用MVC开发web应用程序时,对何时使用web API感到困惑。在上篇文章中,我解释了’WCF,Web API,WCF REST和Web Service间的区别’以及什么时候在其它服务上使用Web API。在这篇文章中,你将学习什么时候选择MVC,什么时候选择Web API。

Asp.Net Web API VS Asp.Net MVC 01.Asp.Net MVC用于创建既返回视图又返回数据。但是Asp.Net Web API能使用简洁明了的方式创建全面的HTTP服务,只返回数据,不返回视图。 02.以往,WebAPI有助于在.NET框架上构建REST-ful服务,它还支持MVC所不支持的自托管的内容协商(决定客户端可以接受的最佳响应格式数据,可以是JSON、XML、ATOM或其他格式的数据) 03.你不用关心Web API根据请求中的Accept头返回特定格式的数据,如JSON、XML或任何其他格式,MVC仅使用JsonResult返回JSON格式的数据

04.在Web API中,动作是基于HTTP请求,但在MVC中,是通过Action名称来反映 05.Asp.Net Web API是一个新的框架,是Asp.Net框架核心的一部分。Web API不同于MVC的是模型绑定,过滤器,路由以及其它MVC特征的特征是存在于System.Web.Http引用中,在MVC中,这些特征是存在于System.Web.Mvc类中。因此,Web API也可以与Asp.Net一起使用,也可作为一个独立的服务层 06.你可以在单个项目中混合使用Web API和MVC controller来处理哪些可能返回JSON、XML或任何其他格式的数据的高级AJAX请求,并构建一个完整的HTTP服务。通常,这被称为Web API自托管 07.当你想在一个项目中同时使用MVC和Web API,想要他们同时都实现授权,那么必须创建两个过滤器,一个用于MVC,另一个用于Web API,因为两者都不同 08.此外,Web API是一种轻量级架构,除了Web应用程序之外,它还可以与智能手机应用程序一起使用

翻译不易,如有转载,请标注出处

————————

原文链接:https://www.dotnettricks.com/learn/webapi/difference-between-aspnet-mvc-and-aspnet-web-api

Since the MVC framework can return JSON data by using jsonresult, it can also handle simple ajax requests. When developing web applications using MVC, many developers are confused about when to use web APIs. In the last article, I explained ‘WCF, web API, the difference between WCF rest and web service’ and when to use web API on other services. In this article, you will learn when to choose MVC and when to choose web API.

Asp. Net Web API VS Asp. Net MVC 01.Asp. Net MVC is used to create both views and data. But ASP Net web API can create a comprehensive HTTP service in a concise and clear way, and only return data without returning view. 02. In the past, webapi has helped in Net framework, which also supports self managed content negotiation (determining the best response format data acceptable to the client, which can be JSON, XML, atom or other formats) 03. You don’t have to worry about the web API returning data in a specific format according to the accept header in the request, such as JSON, XML or any other format. MVC only uses jsonresult to return data in JSON format

04. In Web API, action is based on HTTP request, but in MVC, it is reflected by action name 05.Asp. Net web API is a new framework, ASP Net framework. The difference between web API and MVC is that model binding, filter, routing and other MVC features exist in system Web. In the HTTP reference, in MVC, these features exist in system Web. MVC class. Therefore, the web API can also work with ASP Net can also be used as an independent service layer 06. You can mix web API and MVC controller in a single project to handle Advanced Ajax requests that may return data in JSON, XML or any other format, and build a complete HTTP service. Typically, this is called web API self hosting 07. When you want to use both MVC and web API in a project and want them to implement authorization at the same time, you must create two filters, one for MVC and the other for web API, because they are different 08. In addition, the web API is a lightweight architecture that can be used with smartphone applications in addition to web applications

[< strong > translation is not easy, if reprinted, please mark the source < / strong >]