原文:Refit: The automatic type-safe REST library for .NET Core, Xamarin and .NET
百度翻译:Refit:适用于.NET Core、Xamarin和.NET的自动类型安全REST库
官方网站:https://github.com/reactiveui/refit

using Refit;
namespace RefitDemo
{
public interface IGitHubApi
{
[Get("/WeatherForecast")]
Task<IEnumerable<WeatherForecast>> GetWeatherForecastList();
}
}builder.Services
.AddRefitClient<IGitHubApi>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://localhost:7159"));这里的7159改成你的项目端口
using Microsoft.AspNetCore.Mvc;
using Refit;
namespace RefitDemo.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
private readonly IGitHubApi _gitHubApi;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IGitHubApi gitHubApi)
{
_logger = logger;
_gitHubApi = gitHubApi;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
[HttpPost(Name = "GetTest")]
public async Task<IEnumerable<WeatherForecast>> GetTestAsync()
{
return await _gitHubApi.GetWeatherForecastList();
}
[HttpPost("GetTest1")]
public async Task<IEnumerable<WeatherForecast>> GetTest1Async()
{
var gitHubApi = RestService.For<IGitHubApi>("https://localhost:7159");
return await gitHubApi.GetWeatherForecastList();
}
}
} [HttpPost(Name = "GetTest")]
public async Task<IEnumerable<WeatherForecast>> GetTestAsync()
{
return await _gitHubApi.GetWeatherForecastList();
}
[HttpPost("GetTest1")]
public async Task<IEnumerable<WeatherForecast>> GetTest1Async()
{
var gitHubApi = RestService.For<IGitHubApi>("https://localhost:7159");
return await gitHubApi.GetWeatherForecastList();
}
吴晓阳
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。