RESTful API的POST方法

时间:2014-04-12 16:58:34

标签: c# asp.net rest post

我还是.NET,C#,RESTful API的初学者。现在,我正在学习RESTful API中的POST方法。这是POST方法的示例编码,但我仍然无法得到编码的含义。任何人都能以清晰易懂的方式为我提供编码的每一行解释吗?或者,如果你不介意,你能用评论风格向我解释一下吗?例如:

public string message; //to declare message for what use...`

public class MessageController : ApiController
{
    // GET api/values
    public class MessageToShow
    {
        public string message;
        public string from;
    }

    public List<MessageToShow> Get()
    {   

         var x = new cheeAdDCClf3rfFREntities();

         var  y=x.messages.Take(100);
         List<MessageToShow> messageToShow = new List<MessageToShow>();
         foreach (var xx in y)
         {
             MessageToShow m = new MessageToShow();
             member me = x.members.FirstOrDefault(j => j.ID == xx.from);
             if (me != null)
             {
                 m.from = me.username;
                 m.message = xx.message1;
                 messageToShow.Add(m);
             }

         }
         return messageToShow;

    }

    // POST api/values
    public void Post(int memberid, dynamic value)
    {
        var x = new cheeAdDCClf3rfFREntities();
        message m = new message();
        m.ID = x.messages.Max(record => record.ID) + 1;
        m.from = memberid;
        m.message1 = value.value;
        x.messages.Add(m);
        x.SaveChanges();

    }     
}
}

如果有人愿意将您的编程知识分享给我,我将非常感激。非常感谢!!! ^ _ ^

1 个答案:

答案 0 :(得分:0)

您似乎发布了GETPOST方法的代码。

当我为您的API GET时:

public List<MessageToShow> Get()
{   

     // Looks like EntityFramework? Represents the items already in database
     var x = new cheeAdDCClf3rfFREntities();

     // Take to top 100 item already in the database
     var  y=x.messages.Take(100);

     // Create a new list to hold the messages we will return
     List<MessageToShow> messageToShow = new List<MessageToShow>();

     // For each message in the 100 we just took
     foreach (var xx in y)
     {
         MessageToShow m = new MessageToShow();

         // Get the details of the member that send this message
         member me = x.members.FirstOrDefault(j => j.ID == xx.from);

         // If we found the member, create a message to show
         //  (populating the message and the username of the member
         //  who sent it)
         if (me != null)
         {
             m.from = me.username;
             m.message = xx.message1;
             messageToShow.Add(m);
         }

     }

     // Return the list of messages we just created to the caller of the API
     return messageToShow;
}

当我POST到您的API时,会发生以下情况:

public void Post(int memberid, dynamic value)
{
    // Gets the items already in the database
    var x = new cheeAdDCClf3rfFREntities();

    // Create a new message object
    message m = new message();

    // Find the highest ID already in the database, then add 1. This is the
    //  ID for our new item
    m.ID = x.messages.Max(record => record.ID) + 1;

    // The 'from' property is set to the memberId that the user passed in the POST
    m.from = memberid;

    // The 'message' property is set to whatever dynamic value is passed in the POST
    m.message1 = value.value;

    // Add the message to the database
    x.messages.Add(m);
    x.SaveChanges();
}

要了解有关REST的更多信息,您可以阅读:

A beginners Guide To HTTP and REST

您发布的代码实际上看起来更像EntityFramework,这是一种与数据库交互的方式。它并非特定于API。

您可以在此处找到有关EF的更多信息:

EntityFramework