How do I make calls to a REST API using C#?

后端 未结 15 1768
面向向阳花
面向向阳花 2020-11-22 08:10

This is the code I have so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Net.Http;
usi         


        
15条回答
  •  孤独总比滥情好
    2020-11-22 09:02

    Check out Refit for making calls to REST services from .NET. I've found it very easy to use:

    Refit: The automatic type-safe REST library for .NET Core, Xamarin and .NET

    Refit is a library heavily inspired by Square's Retrofit library, and it turns your REST API into a live interface:

    public interface IGitHubApi {
            [Get("/users/{user}")]
            Task GetUser(string user);
    }
    
    // The RestService class generates an implementation of IGitHubApi
    // that uses HttpClient to make its calls:
    
    var gitHubApi = RestService.For("https://api.github.com");
    
    var octocat = await gitHubApi.GetUser("octocat");
    

提交回复
热议问题