asp.net-mvc ASP.NET WebApi 与 MVC 对比?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11350853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:05:08  来源:igfitidea点击:

ASP.NET WebApi vs MVC ?

asp.net-mvcasp.net-web-api

提问by Hugo Zapata

With ASP.NET MVC controllers you can expose your data in different formats. AspNetWebAPI is designed explicitly for creating API's but i can easily do that with MVC controllers, is not clear to me in what cases it would be better than traditional MVC controllers. I'm interested in scenarios where the benefits of WebApi are obvious and it would be worthy to add another complexity layer to my applications.

使用 ASP.NET MVC 控制器,您可以以不同格式公开数据。AspNetWebAPI 是为创建 API 明确设计的,但我可以使用 MVC 控制器轻松做到这一点,我不清楚在什么情况下它会比传统的 MVC 控制器更好。我对 WebApi 的好处显而易见的场景感兴趣,并且值得为我的应用程序添加另一个复杂层。

Question:what are the advantages and/or disadvantages of using asp.net WebApi in respect to MVC ?

问题:就 MVC 而言,使用 asp.net WebApi 的优点和/或缺点是什么?

采纳答案by Yusubov

WebApi allows to create services that can be exposed over HTTP rather than through a formal service such as WCF or SOAP. Another difference is in the way how WebApi uses Http protocol and makes it truly First class Http citizen.

WebApi 允许创建可以通过 HTTP 而不是通过正式服务(如 WCF 或 SOAP)公开的服务。另一个区别在于 WebApi 如何使用 Http 协议并使其真正成为一流的 Http 公民。

UPDATE:The ASP.NET Core, Web API has been integrated into MVC project type. The ApiControllerclass is consolidated into the Controllerclass. More at: https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

更新:ASP.NET Core、Web API 已集成到 MVC 项目类型中。本ApiController类合并到Controller类。更多信息:https: //wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

A relevant link of comparison, discussions & tutorials:

比较、讨论和教程的相关链接:

enter image description here

在此处输入图片说明

回答by Paolo del Mundo

WebAPI spits out OData, so you get all of the advantages of using OData. For example, with WebAPI you get:

WebAPI 输出 OData,因此您可以获得使用 OData 的所有优势。例如,使用 WebAPI,您将获得:

  • Query options such as $filter, $top, $orderby, etc.
    • With traditional MVC controllers you need to implement these yourself.
  • Standardization of the format
    • There are OData clients that will understand the underlying format of your RESTful API.
  • 查询选项,如 $filter、$top、$orderby 等。
    • 对于传统的 MVC 控制器,您需要自己实现这些。
  • 格式标准化
    • 有一些 OData 客户端可以理解您的 RESTful API 的底层格式。

回答by LostCoder

Similarities

相似之处

1) both inherits from ihhtphandler for the asyncrequest so basically apicontroller or mvc controller both are the wrapper around the web.http

1) 两者都继承自 ihhtphandler 的 asyncrequest 所以基本上 apicontroller 或 mvc 控制器都是围绕 web.http 的包装器

Differences: 1) mvc controller is very heavy if you could go through its definition you can see how many interfaces and the base code it has used, web api is lighter controller and distinguish request by its passed parameters ( yes we can change it too!)

区别: 1)mvc控制器很重,如果你可以通过它的定义,你可以看到它使用了多少接口和基本代码,web api是较轻的控制器,通过传递的参数区分请求(是的,我们也可以改变它! )

2) MVC controller has too many features like it return views, action result , javascript result etc but in web api has either JSON or XML

2) MVC 控制器有太多的特性,比如返回视图、动作结果、javascript 结果等,但在 web api 中有 JSON 或 XML

3) API is for implementing Restful(get, post,put, delete, options) services which can independently can hosted any where without the depending upon views, MVC controller cant support that as it tightly integrated with the views.

3) API 用于实现 Restful(get、post、put、delete、options) 服务,这些服务可以独立托管任何地方而无需依赖视图,MVC 控制器无法支持它,因为它与视图紧密集成。

回答by curiousBoy

Asp.Net Web API VS Asp.Net MVCenter image description here1. Asp.Net MVC is used to create web applications that returns both views and data but Asp.Net Web API is used to create full blown HTTP services with easy and simple way that returns only data not view.

Asp.Net Web API VS Asp.Net MVC 1. Asp.Net MVC 用于创建返回视图和数据的 Web 应用程序,但 Asp.Net Web API 用于创建完整的 HTTP 服务,其简单而简单的方式只返回数据而不返回视图。在此处输入图片说明

2. Web API helps to build REST-ful services over the .NET Framework and it also support content-negotiation(it's about deciding the best response format data that could be acceptable by the client. it could be JSON,XML,ATOM or other formatted data), self hosting which are not in MVC.

2. Web API 有助于在 .NET Framework 上构建 REST-ful 服务,它还支持内容协商(这是关于确定客户端可接受的最佳响应格式数据。它可以是 JSON、XML、ATOM 或其他格式化数据),不在 MVC 中的自托管。

3. Web API also takes care of returning data in particular format like JSON,XML or any other based upon the Accept header in the request and you don't worry about that. MVC only return data in JSON format using JsonResult.

3. Web API 还负责根据请求中的 Accept 标头以特定格式(如 JSON、XML 或任何其他格式)返回数据,您不必担心这一点。MVC 仅使用 JsonResult 以 JSON 格式返回数据。

4. In Web API the request are mapped to the actions based on HTTP verbs but in MVC it is mapped to actions name.

4. 在 Web API 中,请求被映射到基于 HTTP 动词的动作,但在 MVC 中它被映射到动作名称。

5. Asp.Net Web API is new framework and part of the core ASP.NET framework. The model binding, filters, routing and others MVC features exist in Web API are different from MVC and exists in the new System.Web.Http assembly. In MVC, these featues exist with in System.Web.Mvc. Hence Web API can also be used with Asp.Net and as a stand alone service layer.

5. Asp.Net Web API 是新框架,是核心 ASP.NET 框架的一部分。Web API 中存在的模型绑定、过滤器、路由和其他 MVC 功能与 MVC 不同,存在于新的 System.Web.Http 程序集中。在 MVC 中,这些特性存在于 System.Web.Mvc 中。因此,Web API 也可以与 Asp.Net 一起使用并作为独立的服务层使用。

6. You can mix Web API and MVC controller in a single project to handle advanced AJAX requests which may return data in JSON, XML or any others format and building a full blown HTTP service. Typically, this will be called Web API self hosting.

6. 您可以在单个项目中混合 Web API 和 MVC 控制器来处理高级 AJAX 请求,这些请求可能以 JSON、XML 或任何其他格式返回数据并构建完整的 HTTP 服务。通常,这将称为 Web API 自托管。

7. When you have mixed MVC and Web API controller and you want to implement the authorization then you have to create two filters one for MVC and another for Web API since boths are different.

7. 当您混合使用 MVC 和 Web API 控制器并且想要实现授权时,您必须创建两个过滤器,一个用于 MVC,另一个用于 Web API,因为两者不同。

8. Moreover, Web API is light weight architecture and except the web application it can also be used with smart phone apps.

8. 此外,Web API 是轻量级架构,除了 Web 应用程序外,它还可以与智能手机应用程序一起使用。

Original source is here

原始来源在这里

回答by BehrouzMoslem

This image seems to show the differences and similarities in the way the matter. I hope that helps is interesting for me.

这张图片似乎显示了事情的不同之处和相似之处。我希望这对我很有趣。

enter image description here

在此处输入图片说明

回答by What-About-Bob

At some point you might want to forget ASP.NET MVC all together. If you are a .NET developer but you want to build a Single-Page application (using Angular for instance) you'll want the benefits of a RESTful service (WebAPI) without all of the unnecessary bloat that comes with ASP.NET MVC.

在某些时候,您可能想完全忘记 ASP.NET MVC。如果您是 .NET 开发人员,但想要构建单页应用程序(例如使用 Angular),您将需要 RESTful 服务 (WebAPI) 的好处,而没有 ASP.NET MVC 带来的所有不必要的膨胀。

回答by Coder

ASP.NET MVC And ASP.NET Web api both of them are using for different purpose

ASP.NET MVC 和 ASP.NET Web api 它们都用于不同的目的

ASP.NET

ASP.NET

ASP.NET offers three frameworks for creating web applications: Web Forms, ASP.NET MVC, and ASP.NET Web Pages. All three frameworks are stable and mature, and you can create great web applications with any of them. No matter what framework you choose, you will get all the benefits and features of ASP.NET everywhere.

ASP.NET 提供了三个用于创建 Web 应用程序的框架:Web 窗体、ASP.NET MVC 和 ASP.NET 网页。这三个框架都是稳定和成熟的,您可以使用它们中的任何一个来创建出色的 Web 应用程序。无论您选择哪种框架,您都将随时随地获得 ASP.NET 的所有好处和功能。

Each framework targets a different development style. The one you choose depends on a combination of your programming assets (knowledge, skills, and development experience), the type of application you're creating, and the development approach you're comfortable with. All three frameworks will be supported, updated, and improved in future releases of ASP.NET.

每个框架针对不同的开发风格。您选择哪一个取决于您的编程资产(知识、技能和开发经验)、您正在创建的应用程序类型以及您喜欢的开发方法的组合。所有这三个框架都将在 ASP.NET 的未来版本中得到支持、更新和改进。

MVC

MVC

ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and that gives you full control over markup for enjoyable, agile development. ASP.NET MVC includes many features that enable fast, TDD-friendly development for creating sophisticated applications that use the latest web standards.

ASP.NET MVC 为您提供了一种强大的、基于模式的方式来构建动态网站,该方式可以实现关注点的清晰分离,并使您可以完全控制标记以实现愉快、敏捷的开发。ASP.NET MVC 包括许多功能,这些功能支持快速、TDD 友好的开发,用于创建使用最新 Web 标准的复杂应用程序。

Web API

网页接口

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

ASP.NET Web API 是一个框架,可让您轻松构建可访问各种客户端(包括浏览器和移动设备)的 HTTP 服务。ASP.NET Web API 是在 .NET Framework 上构建 RESTful 应用程序的理想平台。

you can read more form here http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-Web-API.html

你可以在这里阅读更多表格 http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-Web-API.html

回答by Murugesan Nataraj

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

ASP.NET MVC 专注于使 HTML 的输出变得容易。ASP.NET Web API 专注于简化原始数据的输出。

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

在 WebForms 世界中,ASP.NET MVC 相当于 .aspx 页面,而 ASP.NET Web API 相当于 .asmx

回答by Nikhil Fegade

Asp.Net MVC is used to create web applications that returns both views and data but Asp.Net Web API is used to create full blown HTTP services with easy and simple way that returns only data not view.

Asp.Net MVC 用于创建返回视图和数据的 Web 应用程序,但 Asp.Net Web API 用于创建完整的 HTTP 服务,其简单而简单的方式只返回数据而不返回视图。

Web API helps to build REST-ful services over the .NET Framework and it also support content-negotiation(it's about deciding the best response format data that could be acceptable by the client. it could be JSON,XML, self hosting which are not in MVC.

Web API 有助于在 .NET Framework 上构建 REST-ful 服务,它还支持内容协商(这是关于确定客户端可接受的最佳响应格式数据。它可以是 JSON、XML、自托管,而不是在MVC中。

Web API also takes care of returning data in particular format like JSON,XML or any other based upon the Accept header in the request and you don't worry about that. MVC only return data in JSON format using JsonResult.

Web API 还负责根据请求中的 Accept 标头以特定格式(如 JSON、XML 或任何其他格式)返回数据,您不必担心这一点。MVC 仅使用 JsonResult 以 JSON 格式返回数据。

回答by Eitan Revach

Usually, WebAPI used for data services where MVC can generate more types of outputs.

通常,WebAPI 用于数据服务,MVC 可以生成更多类型的输出。

WebAPI for sure simplifies the way we can create data services. It's clean and easy for this purpose. MVC come with some more tools around.

WebAPI 无疑简化了我们创建数据服务的方式。为此目的,它既干净又容易。MVC 附带了更多工具。

MVC can generate any output WebAPI can output. Generating outputs from templates can be easily achieved in MVC. I can't find a reason to do so in WebAPI. PHP developers and old ASP programmer might know this attitude from the past, where you can build HTML files involved with C# code inside.

MVC可以生成任何WebAPI可以输出的输出。在 MVC 中可以轻松实现从模板生成输出。我在 WebAPI 中找不到这样做的理由。PHP 开发人员和老 ASP 程序员可能从过去就知道这种态度,您可以在其中构建包含 C# 代码的 HTML 文件。

WebAPI- DATA MVC - DATA, UI/HTML, XHTML, Files, Templates etc..

WebAPI- DATA MVC - 数据、UI/HTML、XHTML、文件、模板等。