我正在研究rest API服务。我有一个数据库表,我想使用rest API添加项目。我有一个WebInvoke
方法来执行PUT
但是如何在REST API服务中调用post方法?即我想要调用createperson
方法(通过传递参数)。
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class RestSerivce : IRestSerivce
{
List<Person> persons = new List<Person>();
int personCount = 0;
public Person CreatePerson(Person createPerson)
{
createPerson.ID = (++personCount).ToString();
persons.Add(createPerson);
InsertDetails(createPerson);
return createPerson;
}
public bool InsertDetails(Person createPerson)
{
string connectionString = "Persist Security Info=False;User ID=sa;Password=P@ssw0rd;Initial Catalog=Demodb;Server=REFL-19";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO PersonDetails (id,Name,Age) VALUES (" + createPerson.ID + ","+createPerson.Name+","+createPerson.Age+");";
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
connection.Open();
cmd.ExecuteNonQuery();
}
return true;
}
}
答案 0 :(得分:1)
如果您遇到服务端,客户端或两者的问题,我对您的问题并不完全确定。我们的团队在第一次接近WCF REST客户端/服务时发现以下内容非常有用。
对于服务端指南,以下Code Project文章提供了托管WCF REST Web服务的全面概述:
http://www.codeproject.com/Articles/571813/A-Beginners-Tutorial-on-Creating-WCF-REST-Services
对于客户端指导,以下链接提供了良好的信息:
http://msdn.microsoft.com/en-US/library/system.net.httpwebrequest.method.aspx
http://forums.asp.net/t/1677895.aspx