1

我想知道是否有人可以确认我使用的命名约定是正确的,我刚刚开始并且真的不想养成一个坏习惯

这就是我所拥有的......(见评论)

基本上我有一个名为 GetTasks 的方法,但 uri 是 Tasks - 我想这是要走的路?

我还有一个名为 GetUser 的方法,其中 Uri 是(复数)Users/{id}

在我继续之前的任何确认都会很棒..谢谢..

这是我目前的方法..

    [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 数据服务。尽管您定义了实体名称,但模式通常是相同的。好消息是您还可以在 GET 的 URL 上定义查询,并且它再次遵循该模式:

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

所以我认为你正朝着正确的方向前进。您是否考虑过 WCF 数据服务?它为您构建这些实体操作——但根据您的要求,它可能不是正确的解决方案。

祝你好运。希望这可以帮助。

于 2010-08-10T15:13:40.530 回答