POST/PUT restful服务的URI模板

MSU*_*SUH 10 api rest wcf c#-4.0

我打算写一个宁静的API,我的要求是在"Transaction"对象上调用方法,我想知道如何使用适当的URI模板调用Post/PUT,以便我可以创建/更新Transaction资源而不使用"verbs"在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);
}
Run Code Online (Sandbox Code Playgroud)

请考虑我想为uri映射应用最佳实践,并且不要在其中使用"动词",只需要"名词".还告诉我客户端如何使用唯一URI访问Post和Put的这些方法.谢谢

VJA*_*JAI 15

您必须映射URI,如下所示Transaction.

通过ID获取交易 - GET - 交易/ ID

创建一个新事务 - POST - 事务

更新交易 - PUT - 交易/ 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)
{
    //
}
Run Code Online (Sandbox Code Playgroud)

客户端如何使用唯一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