Does refit support this usage? #1880
-
|
`[Url("http://localhost:10000/api/user")] } }` Then read the request address from appsettings. json |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
I'm afraid Refit doesn't support either examples. In your case i would do the following: var client = RestService.For<IUserClient>("http://localhost:10000/api/user");
var user = await client.GetUser("LimOpsx", "myPassword");
// calls http://localhost:10000/api/user/getUser?username=LimOpsx&passwordHash=myPassword
public interface IUserClient
{
[Get("/getUser")]
public Task<User> GetUser(string username,string passwordHash);
} |
Beta Was this translation helpful? Give feedback.
-
|
As Timothy said, neither form is supported - there is no For your "many clients, base addresses from appsettings.json" scenario, use appsettings.json: {
"Services": {
"UserApi": "http://localhost:10000/api/user",
"OrderApi": "http://localhost:10001/api/order"
}
}Registration: services.AddRefitClient<IUserClient>()
.ConfigureHttpClient((sp, c) =>
c.BaseAddress = new Uri(
sp.GetRequiredService<IConfiguration>()["Services:UserApi"]!));
services.AddRefitClient<IOrderClient>()
.ConfigureHttpClient((sp, c) =>
c.BaseAddress = new Uri(
sp.GetRequiredService<IConfiguration>()["Services:OrderApi"]!));Then just inject If you are not using DI, pass the address explicitly: |
Beta Was this translation helpful? Give feedback.
As Timothy said, neither form is supported - there is no
[Url]attribute in Refit, and the base address is not read from interface metadata or appsettings automatically. The base address is supplied when the client is created.For your "many clients, base addresses from appsettings.json" scenario, use
IHttpClientFactory+ DI. That is the idiomatic way and avoids hand-rolled reflection.appsettings.json:
{ "Services": { "UserApi": "http://localhost:10000/api/user", "OrderApi": "http://localhost:10001/api/order" } }Registration: