10

私は安らかな API を書くつもりです。私の要件は「Transaction」オブジェクトでメソッドを呼び出すことです。「動詞」を使用せずにトランザクション リソースを作成/更新できるように、適切な URI テンプレートを使用して Post/PUT を呼び出す方法を考えていました。 Uri マッピングで。

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Transaction/{**What to write here ????**}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Transaction AddTransaction(Transaction transaction)
{
    return AddTransactionToRepository(transaction);
}

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/Transaction/{**What to write here ????**}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
public Transaction UpdateTransaction(Transaction transaction)
{
    return UpdateTransactionInRepository(transaction);
}

私は uri マッピングのベスト プラクティスを適用したいと考えており、その中に「動詞」は入れず、「名詞」のみを入れたいと考えています。また、クライアントが一意の URI を使用して Post および Put のこれらのメソッドにアクセスする方法も教えてください。ありがとう

4

3 に答える 3

15

については、以下のように URI をマッピングする必要がありますTransaction

ID でトランザクションを取得 - GET - transaction/id

新しいトランザクションを作成する - POST -トランザクション

トランザクションを更新する - PUT - transaction/id

トランザクションを削除する - DELETE -トランザクション/ID

URI テンプレートを以下のように変更する必要があります

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Transaction", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Transaction AddTransaction(Transaction transaction)
{
    //
}

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/Transaction/{id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
public Transaction UpdateTransaction(int id, Transaction transaction)
{
    //
}

クライアントが一意の URI を使用して Post および Put のこれらのメソッドにアクセスする方法

POST と PUT に固有の URI は必要ありません。URIは同じにすることができます。

参照: http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations

http://msdn.microsoft.com/en-us/library/bb412172(v=vs.90).aspx

于 2012-06-14T16:24:57.330 に答える
2

PUTは、既知のリソースを作成または更新するためのものです。例:PUT / Transactions / 1234

これにより、ID 1234のトランザクションが作成(または既に存在する場合は更新)されます。これは、リソースのURLがわかっている場合にのみPUTを使用できることを意味します。

POSTは、新しい子リソースを作成します。例:POST / Transactions /

これにより、新しいトランザクションリソースが作成されます。

トランザクションを複数形にしたので、コレクションを表すことに注意してください。

C#開発者ではないので、これがWCFにどれほど簡単にマッピングできるかはわかりませんが、このアプローチはテクノロジに依存しません。

于 2012-06-14T13:59:01.237 に答える
-1

適切な URL と API 設計原則を作成するために... この電子ブック (私のものではありません!) を読む必要があります: http://offers.apigee.com/api-design-ebook-rr/

于 2012-12-01T16:02:20.703 に答える