This article shows you how to use feature Add REST API Client in Visual Studio 2017. To practice demo, you should read How to implement swagger ui with web api in asp.net mvc, the article shows you how to create web api in visual studio 2017.
Next, create a new windows forms project and design a simple UI as shown below.
Right click on your project->Add->REST API Client..., enter your Swagger URL and Client namespace, then click the OK button to generate the REST API client.
Swagger is an agile technology standard that enables the discovery of REST APIs, providing a way for any software to determine the features of a REST API.
Click OK button to generate your API Client
You can see your api client automatically generate in your project.
Add code to handler load button event
private void btnLoad_Click(object sender, EventArgs e)
{
TestServiceClient client = new TestServiceClient(new Uri("http://localhost:49814"), new AnonymousCredential());
//var headers = client.HttpClient.DefaultRequestHeaders;
//string accessToken = "";
//headers.Remove("Authorization");
//headers.Add("Authorization", $"Bearer {accessToken}");
CustomerOperations operations = new CustomerOperations(client);
customerBindingSource.DataSource = operations.GetCustomers();
}
Create an AnonymousCredential class inheriting from ServiceClientCredentials
public class AnonymousCredential : ServiceClientCredentials
{
}
Press F5 to run your project