简体   繁体   English

MVC 5项目和Web Api项目之间的区别

[英]Difference between MVC 5 Project and Web Api Project

I am new to ASP.NET MVC and Web API and trying to get the basics. 我是ASP.NET MVCWeb API的新手,正在尝试获取基础知识。 AFAIK, we have project templates in VS 2013, named as MVC , Web API and Both of them together . AFAIK,我们在VS 2013中有项目模板,分别称为MVCWeb APIBoth of them together

I have gone through the tutorials and learned that we can make an API by using MVC alone as well as with Web API Template. 我遍历了教程,了解到我们可以单独使用MVC以及使用Web API模板来制作API。

So, What are the differences between these, based on Architecture and Usage ? 那么,基于体系结构用法 ,两者之间有什么区别?

Basically, a Web API controller is an MVC controller, which uses HttpMessageResponse as the base type of its response, instead of ActionResponse . 基本上, Web API控制器是MVC控制器,它使用HttpMessageResponse作为其响应的基本类型,而不是ActionResponse They are the same in most other respects. 它们在大多数其他方面是相同的。 The main difference between the project types is that the MVC Application project type adds web specific things like default CSS , JavaScript files and other resources needed for a web site, which are not needed for an API. 项目类型之间的主要区别在于, MVC Application项目类型添加了Web特定的东西,例如默认CSSJavaScript文件和网站所需的其他资源,而API则不需要。

MVC is used for creating web sites. MVC用于创建网站。 In this case Controllers usually return a View (ie HTML response) to browser requests. 在这种情况下, Controllers通常会向浏览器请求返回一个View (即HTML响应)。 Web APIs on the other hand are usually made to be consumed by other applications. 另一方面,通常使Web API被其他应用程序使用。 If you want to allow other applications to access your data / functionality, you can create a Web API to facilitate this access. 如果要允许其他应用程序访问您的数据/功能,则可以创建一个Web API来促进这种访问。 For example, Facebook has an API in order to allow App developers to access information about users using the App. 例如,Facebook有一个API,以便允许App开发人员访问有关使用该App的用户的信息。 Web APIs don't have to be for public consumption. Web API不必供公众使用。 You can also create an API to support your own applications. 您还可以创建一个API来支持自己的应用程序。 For example, we created a Web API to support the AJAX functionality of our MVC web site. 例如,我们创建了一个Web API以支持MVC网站的AJAX功能。

Microsoft changed the way they present the different templates. Microsoft更改了他们呈现不同模板的方式。 Now instead of using different templates for different project types, they encourage developers to mix ASP.NET technologies inside the same project as needed. 现在,他们鼓励开发人员根据需要在同一个项目中混合使用ASP.NET技术,而不是对不同的项目类型使用不同的模板。 Microsoft calls this vNext . Microsoft将其称为vNext

UPDATE: For ASP.NET Core, Web API has been integrated into the MVC 6 project type and the ApiController class is consolidated into the Controller class. 更新:对于ASP.NET Core,Web API已集成到MVC 6项目类型中,并且ApiController类已合并到Controller类中。 Further details at: https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6 有关更多详细信息,请访问: https//wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

My two cents... 我的两分钱

  1. In ASP.Net MVC – the MVC's Controller decides what should be the View - ie, the controller decides what the user should “see” (based on the current scenario or context), when they make a request. ASP.Net MVC中-MVC的Controller决定应该是什么View -即,控制器决定用户在发出请求时应“看到”的内容(基于当前方案或上下文)。
  2. In ASP.Net Web Forms , the ASPX pages decides what the user should “see” when they make a request. ASP.Net Web窗体中ASPX页决定用户提出请求时应“看到”的内容。
  3. But in Web API , there is no control/power to any of the Web API's features to decide what the user should “see” when they make a request. 但是在Web API中 ,Web API的任何功能都无法控制/确定用户在发出请求时应“看到”的内容。

Web API is NOT a technology tied up with websites only. Web API不是仅与网站捆绑在一起的技术。 It can be used for multiple purposes – not only websites. 它可以用于多种目的–不仅限于网站。 So it doesn't know the meaning of rendering 所以它不知道rendering的含义

Further Reading 进一步阅读

  1. Planning Web Solutions Today: Web Forms, ASP.NET MVC, Web API, and OWIN. 今天规划Web解决方案:Web窗体,ASP.NET MVC,Web API和OWIN。
  2. WCF or ASP.NET Web APIs? WCF或ASP.NET Web API? My two cents on the subject 我在这个问题上的两分钱
  3. The Next Generation of .NET – ASP.NET vNext 下一代.NET – ASP.NET vNext
  4. Getting Started with ASP.NET MVC 6 ASP.NET MVC 6入门

MVC controller derived from controller class. 从控制器类派生的MVC控制器。 In Mvc you can returns views. 在Mvc中,您可以返回视图。 Mvc achitecture uses to create an application. Mvc体系结构用于创建应用程序。 However Web apis are used to provide data to various application. 但是,Web API用于向各种应用程序提供数据。

Web Api drives from Api controller and it doesn't return view. Web Api从Api控制器驱动器,并且不返回视图。

Note: You can also create Web Api from MVC controller but you need to return result as JsonResult or other web api supported return types. 注意:您也可以从MVC控制器创建Web Api,但需要以JsonResult或其他受Web API支持的返回类型返回结果。

In addition to answers already provided here, its worth noting any controller which inherits from ApiController and having an action with Http verb POST can only have one [FromBody] input parameter. 除了此处已提供的答案外,值得注意的是,任何从ApiController继承并具有Http动词POST动作的控制器只能具有一个[FromBody]输入参数。 If using a MVC controller (deriving from 'Controller') you can have many post input parameters. 如果使用MVC控制器(源自“ Controller”),则可以具有许多后期输入参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM