本文介绍了实现版本与WCF或ASP.Net网页API一个RESTful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我读过很多关于版本一个宁静的API,我决定不通过URI版本的服务,但使用mediatypes(格式和架构在请求接受头):

Assume i've read a lot about versioning a restful api, and I decided to not version the the service through the uri, but using mediatypes (format and schema in the request accept header):

什么是实现一个WCF服务或Web API服务,以服务于URI定义所请求的资源,格式(如应用程序/ JSON)和架构/版本(例如玩家-V2)请求的最佳方式在接受头?

What would be the best way to implement a wcf service or a web api service to serve requests defining the requested resource in the uri, the format (eg. application/json) and the schema/version (eg player-v2) in the accept header?

WCF允许我基于URI的路线,而不是基于标题。所以我不能正确路由。

WCF allows me to route based on the uri, but not based on headers. So I cannot route properly.

网页API可以让我定义自定义mediatypeformatters,路由请求的格式,而不是架构(如返回类型PlayerV1或PlayerV2)。

Web Api allows me to define custom mediatypeformatters, routing for the requested format, but not the schema (eg. return type PlayerV1 or PlayerV2).

我想实现服务(无论是与WCF或Web API)当中,为此请求(伪code):

I would like to implement a service(either with WCF or Web Api) which, for this request (Pseudo code):

api.myservice.com/players/123 Accept format=application/json; schema=player-v1

返回PlayerV1实体,JSON格式

returns a PlayerV1 entity, in json format

和此请求:

api.myservice.com/players/123 Accept format=application/json; schema=player-v2

返回PlayerV2实体,JSON格式。

returns a PlayerV2 entity, in json format.

这是如何实现这个任何提示?

Any tips on how to implement this?

修改:为了澄清我为什么要使用内容协商来处理的版本,在这里看到:的

EDIT: To clarify why I want to use content negotiation to deal with versions, see here: REST API Design: Put the "Type" in "Content-Type".

推荐答案

你所带来这里不看我,版本,但它是更多的内容协商。接受资源的格式的客户端头前presses愿望。服务器应授予愿望还是回到406。因此,如果我们需要更多的合同的概念(虽然网页API unline RPC没有定义),然后用资源更加牢固。

What you are bringing here does not look to me as versioning but it is is more of content negotiation. Accept header expresses wishes of the client on the format of the resource. Server should grant the wishes or return 406. So if we need more of a concept of Contract (although Web API unline RPC does not define one) then using resource is more solid.

有关版本的最佳实践还没有得到充分的讨论,但最REST爱好者相信使用在URL中的版本是去(如 HTTP方式://服务器/api/1.0.3 /...)。这在你的方法也使得我更有意义,因为使用内容协商服务器,以保持向后兼容性,我只能想象在服务器上code会得到更多,更复杂。使用URL的方式,你可以一刀两断:老客户可以愉快地使用previous,而新客户可以享受新的API带来的好处。

The best practices for versioning have yet to be discussed fully but most REST enthusiast believe using the version in the URL is the way to go (e.g. http://server/api/1.0.3/...). This also makes more sense to me since in your approach using content negotiation server has to keep backward compatibility and I can only imagine the code at the server will get more and more complex. With using URL approach, you can make a clean break: old clients can happily use previous while new clients can enjoy the benefits of new API.

OK,现在的问题已经改变为以RESTful AP实现内容协商。

OK, now the question has changed to "Implementing content-negotiation in a RESTful AP".

基本上,如果谈判的内容涉及到资源的唯一格式,实施或使用正确的媒体类型格式就够了。例如,如果内容协商涉及返回JSON或XML。在这种情况下,控制器是无视以内容的谈判。

Basically, if content negotiation involves only the format of the resource, implementing or using the right media type formatter is enough. For example, if content negotiation involves returning JSON or XML. In these cases, controller is oblivious to content negotiations.

控制器需要知道请求协商。在这种情况下,需要从该请求中提取,并通过在作为参数从请求参数。例如,假设一个控制器上的这个动作:

Controller needs to be aware of the request negotiation. In this case, parameters from the request needs to be extracted from the request and passed in as parameter. For example, let's imagine this action on a controller:

public Player Get(string schemaVersion)
{
    ...
}

在这种情况下,我会用经典的MVC风格的价值提供者(见的后 - 这是对MVC的Web,但API的价值提供商类似于):

In this case, I would use classic MVC style value providers (See Brad Wilson's post on ValueProviders - this is on MVC but Web API's value provider looks similar):

public Player Get([ValueProvider(typeof(RequestHeadersSchemaValueProviderFactory))]string schemaVersion)
{
    ...
}

这篇关于实现版本与WCF或ASP.Net网页API一个RESTful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:48