1

私が使用している命名規則が正しいことを誰かが確認できるのだろうか.

これが私が持っているものです...(コメントを参照)

基本的に GetTasks というメソッドがありますが、URI は Tasks です。

また、Uri が (複数の) Users/{id} である GetUser というメソッドがあります。

続行する前の確認は素晴らしいでしょう..ありがとう..

ここに私が現在持っている方法があります..

    [WebGet(UriTemplate = "Tasks")]
    public List<SampleItem> GetTasks()  //RETURNS a COLLECTION
    {
        // TODO: Replace the current implementation to return a collection of SampleItem instances
        return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
    }


    [WebGet(UriTemplate = "Users/{id}")]
    public SampleItem GetUser(string id) // RETURNS A USER
    {
        // TODO: Return the instance of SampleItem with the given id
        //throw new NotImplementedException();
        return new SampleItem() {Id = 1, StringValue = "Hello"};
    }

    [WebInvoke(UriTemplate = "Users/{id}", Method = "PUT")]
    public SampleItem UpdateUser(string id, SampleItem instance) // UPDATES A USER
    {
        // TODO: Update the given instance of SampleItem in the collection
        throw new NotImplementedException();
    }

    [WebInvoke(UriTemplate = "Users/{id}", Method = "DELETE")]
    public void DeleteUser(string id) // DELETES A USER
    {
        // TODO: Remove the instance of SampleItem with the given id from the collection
        throw new NotImplementedException();
    }
4

1 に答える 1

1

まあ、時には模倣が最高のお世辞です。StackOverflow を見るとusers/{userid}/...、URI 規則に使用されています。一般に、エンティティに複数形を使用し、次のセグメント (存在する場合) にアクションを説明させるのは、かなり標準的なようです。これは通常、RESTful サービスを定義する際に私が進む方向です。

GET      /Users         -- return all users
GET      /Users/{id}    -- return a single user
POST     /Users         -- insert a user
PUT      /Users/{id}    -- update a user
DELETE   /Users/{id}    -- delete a user

これは、WCF Data Services にも非常に適しています。エンティティ名を定義しますが、パターンは通常同じです。良い点は、GET の URL でクエリを定義することもできることです。これもパターンに従います。

GET     /Users?$top=10&filter=name eq 'Steve'  -- return top 10 users who's name is steve

だから、あなたは正しい方向に進んでいると思います。WCF Data Services を検討したことがありますか? これらのエンティティ操作を構築しますが、要件によっては適切なソリューションではない場合があります。

幸運を。お役に立てれば。

于 2010-08-10T15:13:40.530 に答える