【问题标题】:WCF Rest naming conventions for methods and URIs?方法和 URI 的 WCF Rest 命名约定?
【发布时间】:2011-03-26 17:55:10
【问题描述】:

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

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

基本上我有一个名为 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();
    }

【问题讨论】:

  • 我有点困惑的一件事是“GetUser”,其中 Uri 是(复数)Users/{id} 还是应该是(单数)User/{id}

标签: wcf visual-studio-2010 rest wcf-rest


【解决方案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 数据服务?它为您构建这些实体操作——但根据您的要求,它可能不是正确的解决方案。

祝你好运。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多