I’m relatively new to c# and working with API’s. I’ve created a simple windows form in VS and I’m trying to connect t a rest service to retrieve information based on a search condition (e.g ID number) and display everything in a data grid. I’ve been looking for examples of what I’m trying to achieve with very little success. The idea is to enable a user to enter an ID number inside a text box and click a “search” button which will then connect to the rest service and retrieve all the information related to that specific ID number and display it all in a data grid with column names. Is this possible? Can anyone advise me on how to establish my connection to the rest service?
Asked
Active
Viewed 77 times
1 Answers
1
Try this. Path1 is your connection: The Class ApiResult is just a class with a List of ArticleApiModel.
public static List<ArticleApiModel> GetArticles (int id)
{
try
{
var task = Task<List<ArticleApiModel>>.Run(async () =>
{
using (HttpClient client = new HttpClient())
{
var response = await client.GetAsync(path1 + "/api/articles/",id);
if (response != null)
{
var jsonString = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<ApiResult>(jsonString);
return result.Result;
}
}
return null;
});
task.Wait();
return task.Result;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return null;
}

samuel gast
- 331
- 4
- 17
-
I’m getting an error that says “ApiResult does not contain a definition and no extension method ‘Result’ accepting a first argument of type ‘ApiResult’ could be found (are you missing a using directive or an assembly reference?)” – Mg Mogaki Jan 23 '19 at 13:49
-
So the ApiResult is a class I made where I have a list of ArticleApiModel. So you should make a class that contains a list of your model and then replace it with yours. – samuel gast Jan 23 '19 at 14:19