DotNet tool to generate Refit HTTP client types from ASP.NET Core API controllers.
Tool requires .NET 8 runtime installed, and it supports projects with ASP.NET Core 8, 7 or 6.
Install dotnet tool from Nuget:
dotnet tool install GenerateAspNetCoreClient.Refit -g
Then execute the following in the directory with your Web project:
dotnet-generate-client MyApiProjectPath -o OutPath -n My.Client.Namespace
The tool will generate Refit interfaces based on the endpoints defined in your project. Please note that only .cs files are created, you still need to add the project file, with project references for models (if any needed), and Refit package reference.
Based on the following controller:
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
[HttpGet]
public async Task<ActionResult<IEnumerable<WeatherForecast>>> Get()
{...
}
[HttpGet("{id}")]
public async Task<WeatherForecast> Get(Guid id)
{...
}
}
IWeatherForecastApi.cs
file is created:
//<auto-generated />
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Refit;
using TestWebApi.Models;
namespace Test.Name.Space
{
public interface IWeatherForecastApi
{
[Get("/WeatherForecast")]
Task<IEnumerable<WeatherForecast>> Get();
[Get("/WeatherForecast/{id}")]
Task<WeatherForecast> Get(Guid id);
}
}
-o, --out-path Required. Relative out path for generated files.
-n, --namespace Required. Namespace for generated client types.
--environment ASPNETCORE_ENVIRONMENT to set during generation.
--type-name-pattern (Default: I[controller]Api) Pattern by which client types are named.
--access-modifier (Default: public) Access modifier used for generated clients.
--add-cancellation-token (Default: false) Add CancellationToken parameters to all endpoints.
--use-query-models (Default: false) Use query container type parameter (as defined in the endpoint) instead
of separate parameters.
--use-api-responses (Default: false) Use Task<IApiResponse<T>> return types for endpoints.
--exclude-types Exclude all controller types with substring in full name (including namespace).
--exclude-paths Exclude all endpoints with substring in relative path.
--include-types Include only controller types with substring in full name (including namespace).
--include-paths Include only endpoints with substring in relative path.