MVC 5 项目和 Web Api 项目的区别 [英] Difference between MVC 5 Project and Web Api Project

查看:42
本文介绍了MVC 5 项目和 Web Api 项目的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ASP.NET MVCWeb API 的新手,正在尝试了解基础知识.AFAIK,我们在 VS 2013 中有项目模板,命名为 MVCWeb API它们一起.

I am new to ASP.NET MVC and Web API and trying to get the basics. AFAIK, we have project templates in VS 2013, named as MVC, Web API and Both of them together.

我已经阅读了教程并了解到我们可以单独使用 MVC 以及使用 Web API 模板来制作 API.

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.

那么,基于架构使用,这些之间有什么区别?

So, What are the differences between these, based on Architecture and Usage?

推荐答案

基本上,Web API 控制器是一个 MVC 控制器,它使用 HttpMessageResponse 作为其响应的基本类型,而不是 ActionResponse.它们在大多数其他方面是相同的.项目类型之间的主要区别在于 MVC Application 项目类型添加了 web 特定的东西,如默认的 CSSJavaScript 文件和其他所需的资源API 不需要的网站.

Basically, a Web API controller is an MVC controller, which uses HttpMessageResponse as the base type of its response, instead of 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 用于创建网站.在这种情况下,Controllers 通常会向浏览器请求返回一个 View(即 HTML 响应).另一方面,Web API 通常被其他应用程序使用.如果您想允许其他应用程序访问您的数据/功能,您可以创建一个 Web API 来促进此访问.例如,Facebook 有一个 API 以允许应用程序开发人员访问有关使用该应用程序的用户的信息.Web API 不必供公众使用.您还可以创建 API 来支持您自己的应用程序.例如,我们创建了一个 Web API 来支持我们 MVC 网站的 AJAX 功能.

MVC is used for creating web sites. In this case Controllers usually return a View (i.e. HTML response) to browser requests. Web APIs on the other hand are usually made to be consumed by other applications. If you want to allow other applications to access your data / functionality, you can create a Web API to facilitate this access. For example, Facebook has an API in order to allow App developers to access information about users using the App. Web APIs don't have to be for public consumption. You can also create an API to support your own applications. For example, we created a Web API to support the AJAX functionality of our MVC web site.

微软改变了他们展示不同模板的方式.现在,他们不再为不同的项目类型使用不同的模板,而是鼓励开发人员根据需要在同一项目中混合使用 ASP.NET 技术.Microsoft 将此称为 vNext.

Microsoft changed the way they present the different templates. Now instead of using different templates for different project types, they encourage developers to mix ASP.NET technologies inside the same project as needed. Microsoft calls this vNext.

更新:对于 ASP.NET Core,Web API 已集成到 MVC 6 项目类型中,ApiController 类已合并到 Controller类.更多详细信息,请访问:https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

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. Further details at: https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

这篇关于MVC 5 项目和 Web Api 项目的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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