我是web api的新手。很抱歉问一个非常基本的问题。假设这是我的web api类结构
public class Person
{
string FirstName;
string LastName;
public Person(string fn, string ln)
{
FirstName = fn;
LastName = ln;
}
}
public class Team
{
string TeamName;
Person TeamLeader;
List<Person> TeamMembers;
public Team(string name, Person lead, List<Person> members)
{
TeamName = name;
TeamLeader = lead;
TeamMembers = members;
}
}
public class Response
{
int ResponseCode;
string ResponseMessage;
object ResponsePayload;
public Response(int code, string message, object payload)
{
ResponseCode = code;
ResponseMessage = message;
ResponsePayload = payload;
}
}
public class PersonController : ApiController
{
public Response Get()
{
Person tom = new Person("Tom", "Cruise");
Response response = new Response(1, "It works!", tom);
return response;
}
}
public class TeamController : ApiController
{
public Response Get()
{
Person tom = new Person("Tom", "Cruise");
Person cindy = new Person("Cindy", "Cullen");
Person jason = new Person("Jason","Lien");
Team awesome = new Team("Awesome", jason, new List<Person>(){tom,cindy});
Response response = new Response(1, "It works!", awesome);
return response;
}
}
现在告诉我如何从c#winform应用程序中调用PersonController and TeamController Get
函数,并将数据传递给web api函数。
我是web api的新手,我需要开发一个web api,它将在一个单独的PC上运行,并且web api将由几个winform客户端使用。
winform客户端会将人员和团队数据发送到web api,web api将数据保存到db中,并且win time客户端再次从web api获取数据。我正在寻找一个示例,向我展示如何编写代码,这将创建winform客户端和web api之间的交互。
当我们从winform客户端向web api发送数据时,我是否需要准备json字符串并将其发送到web api,否则它将在现场后自动完成?
需要一点指导线。感谢
答案 0 :(得分:0)
有一个很好的教程,介绍如何使用非mvc应用程序中的WebApi:http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client
查看Microsoft.AspNet.WebApi.Client包(https://www.nuget.org/packages/microsoft.aspnet.webapi.client/),它有助于内容协商和JSON编写/解析。