簡體   English   中英

如何從Web API調用和讀取響應

[英]How to call and read response from web api

我是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的新手,我需要開發將在單獨的PC中運行的Web API,並且該Web API將被多個Winform客戶端使用。

winform客戶端會將個人和團隊數據發送到Web api,該Web api將數據保存到db中,並且再過一段時間,winform客戶端將從Web api獲取數據。 我正在尋找一個示例,該示例向我展示了如何編寫代碼來創建winform客戶端和Web api之間的交互。

當我們將數據從Winform客戶端發送到Web api時,我是否需要准備json字符串並將其發送到Web api,否則它將在后台自動完成?

需要一點指導方針。 謝謝

關於如何從非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編寫/解析。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM