You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apologies if this has been asked in a different way - I did take a look at the discussions and the documentation and can't quite see anything that fits.
I have a Blazor app - that's not too important, other than to signify that I have a front end - calling into a backend API.
So my API Controller start like this
[ApiController]
[Authorize(UserType.Admin)]
[Route("api/settings/roles")]
public class SettingsRolesController(DbContext tenantDb) : ControllerBase
And has an endpoint for Adding or Updating an existing role like this
[HttpPost]
public async Task<RoleModel> AddOrUpdateRole(RoleModel modelRole)
{
// DB Role
Role dbRole;
if(modelRole.Id == -1)
{
// First we add the new role to the database as we need it's ID for the role
dbRole = new Role
{
.. fill in details ...
};
}
_tenantDb.Roles.Add(dbRole);
await _tenantDb.SaveChangesAsync(true);
modelRole.Id = dbRole.Id;
await _tenantDb.SaveChangesAsync();
return modelRole;
}
RoleModel by the way is a ViewModel used by the FrontEnd - Role is the DB entity type
The main thing to spot is that the returned RoleModel instance will have the ID of the Database entity set.
Now in my Refit definition I set the interface like this
If I was using a HttpClient manually - I could make a call to the API endpoint - get the response and IF all was good I could update the 'role' object with the returned ID of the RoleModel in the response.
e.g.
async Task<RoleModel> AddOrUpdateRole(RoleModel role)
{
HttpClient client = <get client and base address and headers etc.>
using HttpResponseMessage response = await client.PostAsJsonAsync(
"/api/settings/roles",
role);
response.EnsureSuccessStatusCode()
.WriteRequestToConsole();
var newRoleModel= await response.Content.ReadFromJsonAsync<RoleModel >();
role.Id = newRoleModel.Id;
}
That would allow me in my Blazor code to do this
role = new RoleModel {<set parameters>};
await ApiService.AddOrUpdateRole(role);
The instance of 'role' would have the id set after the call.
Using Refit however I would have to do this
role = new RoleModel {<set parameters>};
role = await IRefitInterface.AddOrUpdateRole(role);
Note that I have to set my current role to the return of the call into refit.
If I forget to do that, then my current 'role' object will not have the id set.
Is there something I am missing in the config or attributes of Refit that would allow me to update the body if it has the same return type?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Apologies if this has been asked in a different way - I did take a look at the discussions and the documentation and can't quite see anything that fits.
I have a Blazor app - that's not too important, other than to signify that I have a front end - calling into a backend API.
So my API Controller start like this
And has an endpoint for Adding or Updating an existing role like this
RoleModel by the way is a ViewModel used by the FrontEnd - Role is the DB entity type
The main thing to spot is that the returned RoleModel instance will have the ID of the Database entity set.
Now in my Refit definition I set the interface like this
So now onto my question:
If I was using a HttpClient manually - I could make a call to the API endpoint - get the response and IF all was good I could update the 'role' object with the returned ID of the RoleModel in the response.
e.g.
That would allow me in my Blazor code to do this
The instance of 'role' would have the id set after the call.
Using Refit however I would have to do this
Note that I have to set my current role to the return of the call into refit.
If I forget to do that, then my current 'role' object will not have the id set.
Is there something I am missing in the config or attributes of Refit that would allow me to update the body if it has the same return type?
Many thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions