Reputation: 587
I am studying rest API service. I have a database table that I want to add the item to using a rest API. I have an WebInvoke
method to do PUT
but how do I call a post method in REST API service? i.e I to want call createperson
method(by passing parameters).
[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;
}
}
Upvotes: 0
Views: 2588
Reputation: 7067
I was not entirely sure from your question if you are having issues with the service-side, client-side or both. Our team found the following helpful when approaching WCF REST client/services for the first time.
For service-side guidance, the following Code Project article provides a comprehensive overview of hosting a WCF REST web service:
http://www.codeproject.com/Articles/571813/A-Beginners-Tutorial-on-Creating-WCF-REST-Services
For client-side guidance, the following links provide good information:
http://msdn.microsoft.com/en-US/library/system.net.httpwebrequest.method.aspx
http://forums.asp.net/t/1677895.aspx
Good luck.
Upvotes: 1