0

我已经阅读了有关 asp.net Web API 的信息,但我不确定我是否理解 Web API 工具背后的概念。现在假设我已经定义了一个从 APIController 派生的控制器,我可以使用 URL 调用这个控制器,然后控制器会将 JSON 结果返回给客户端,,,

那么这是否意味着我的 asp.net MVC Web 应用程序中的 APIController 被公开为 RESTfull Web 服务? BR

4

3 回答 3

2

Whether or not your API is considered 'RESTfull' is a question of design, not a question of which libraries/project templates are used. Remember that the whole point of an API is to expose a simple programming interface - how it's implemented behind the scenes isn't important in that regard.

However, the WebAPI project template does lend itself well to designing a restfull service due to its built in design principles.

The basic idea behind a REST API is:

  • Use HTTP protocol (which WebAPI does, so check)
  • Expose a simple API for handling data, usually with POST/GET/PUT/DELETE methods tied to data handling (this is what the WebAPI design lends itself well to)
  • Transfer data in an interoperable format (JSON, XML, also easily doable in WebAPI)

It's not much different from designing a REST API using basic MVC Controller/Actions, WebAPI just makes it a bit easier.

Here's a nice video series: http://www.asp.net/web-api/videos/getting-started/your-first-web-api

于 2012-07-24T07:56:28.507 回答
1

i just want to add to Anders

REST stands for Representational state transfer
so basically in architecture you choose a resource like http://www.example.com/USER
and you instruct the server to do operations on the resource. Specified by the type of HTTP request being sent
going by what web api comes out of the box
POST - add a new user
PUT - edits an existing users info
GET - simply retrives user(s) info
DELETE - deletes users info
and this is REST

The type of data being sent or recived (JSON or XML). isnt a part of rest specifications. also you can decide incase of asp.net web api data is returned and sent in what format by using Accept and Content-Type headers with your request

于 2012-07-24T08:32:17.633 回答
0

Apicontroller 与 mvc Controller 完全不同,它来自 Wcf WebApi,但对开发人员来说与经典的 asp.net 控制器非常相似。重点是加快提供restfull服务。

因此,您只需像往常一样多分配一个路由,制作一些控制器,而不是采用 querystring 和 body json(默认)并自动转换为 Poco 类,并自动返回另一个 Poco 类,序列化为 jsion。

于 2012-07-24T07:59:22.807 回答