BlazorRest 1.6.1
dotnet add package BlazorRest --version 1.6.1
NuGet\Install-Package BlazorRest -Version 1.6.1
<PackageReference Include="BlazorRest" Version="1.6.1" />
<PackageVersion Include="BlazorRest" Version="1.6.1" />
<PackageReference Include="BlazorRest" />
paket add BlazorRest --version 1.6.1
#r "nuget: BlazorRest, 1.6.1"
#:package BlazorRest@1.6.1
#addin nuget:?package=BlazorRest&version=1.6.1
#tool nuget:?package=BlazorRest&version=1.6.1
BlazorRest
blazor rest is a library for sending http requests in Blazor WebAssembly in the simplest way. blazorrest also gives you the ability to intercept requests, response, and errors that you can customize them. You will no longer have to serialize or deserialize data or even add an auhtorization header.
Installing
To install the package add the following line to you csproj file replacing x.x.x with the latest version number (found at the top of this file
<PackageReference Include="BlazorRest" Version="x.x.x" />
You can also install via the .NET CLI with the following command:
dotnet add package BlazorRest
Setup
In the program.cs file, you can register in the simplest way as follows
builder.Services.AddBlazorRest(opt =>
{
opt.BaseUri = new Uri("http://localhost:54240");
});
If it is registered above, you can only send http requests without any other functionality
But to send Bearer tokens automatically, you can implement the IJwtService interface
public interface IJwtService
{
ValueTask SetTokenAsync(string? jwtToken);
ValueTask<string?> GetTokenAsync();
ValueTask SetRefereshTokenAsync(string? refreshToken);
ValueTask<string?> GetRefereshTokenAsync();
}
And register the subclass that it has implemented IJwtTokenService interface as follows at the time of registration
builder.Services.AddBlazorRest(opt =>
{
opt.BaseUri = new Uri("http://localhost:54240");
opt.UseJwtService<JwtService>();//you can implement IJwtService and use like this
});
implement the IRequestInterceptor interface To intercept requests.
public interface IRequestInterceptor
{
HttpRequestMessage InterceptRequest(HttpRequestMessage request);
}
implement the IResponseInterceptor interface To intercept response.
public interface IResponseInterceptor
{
HttpResponseMessage InterceptResponse(HttpResponseMessage response);
}
implement the IErrorInterceptor interface For Intercept requests, their status code not equal to 200.
public interface IErrorInterceptor
{
Task InterceptError(ErrorInterceptorModel? error);
}
The most complete way to register blazorrest is as follows.
builder.Services.AddBlazorRest(opt =>
{
opt.BaseUri = new Uri("http://localhost:54240");
opt.UseJwtService<JwtService>(); //you can implement IJwtService and use like this
opt.UseRequestInterceptor<RequestInterceptor>();
opt.UseResponseInterceptor<ResponseInterceptor>();
opt.UseErrorInterceptor<ErrorInterceptor>();
});
sample
login
public class AccountService : IAccountService
{
private readonly IBlazorRest _blazorRest;
private readonly IJwtService _jwt;
public AccountService(IJwtService jwt,IBlazorRest blazorRest)
{
_jwt = jwt;
_blazorRest = blazorRest;
}
public async Task Login(UserLoginDto loginDto)
{
var message =new BlazorRestMessage("/auth/login", HttpMethod.Post)
{
Content = new BlazorRest.RequestContents.JsonContent(loginDto)
};
var result = await _blazorRest.SendAsync<LoginResponse>(message);
if (!result.IsSuccessful)
{
//do somthing
}
await _jwt.SetTokenAsync(result.Data.Token);
}
}
UploadFile
public async Task UploadAvatarAsync(IBrowserFile ProfileImage)
{
var message = new BlazorRestMessage("/user/avatar", HttpMethod.Post)
{
Content = new FileContent(ProfileImage, "FileNameInFormData")
};
var result = await _blazorRest.SendAsync(message);
}
upload file with model
public async Task EditUserAsync(EditProfileDto editUserDto, IBrowserFile ProfileImage)
{
var message = new BlazorRestMessage("/user", HttpMethod.Post)
{
Content = new FileWithModelContent(ProfileImage, nameof(ProfileImage), editUserDto)
};
var result = await _blazorRest.SendAsync<EditProfileResponse>(message);
//or
//var result = await _blazorRest.SendAsync(message);
}
Simple Get Request
public async Task<IEnumerable<WeatherForecast>> Get()
{
var result = await _blazorRest.GetAsync<WeatherForecast[]>("WeatherForecast");
if (!result.IsSuccessful)
{
//do somthing
}
return result.Data;
}
Get Request with Response Options
public async Task<IEnumerable<WeatherForecast>> Get()
{
var result = await _blazorRest.GetAsync<WeatherForecast[]>("WeatherForecast", new ResponseOptions
{
SerializerOptions = new System.Text.Json.JsonSerializerOptions
{
PropertyNameCaseInsensitive = false
}
});
if (!result.IsSuccessful)
{
//do somthing
}
return result.Data;
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.AspNetCore.Components.Web (>= 10.0.0)
-
net5.0
- Microsoft.AspNetCore.Components.Web (>= 5.0.17)
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.11)
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.0)
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.0)
-
net9.0
- Microsoft.AspNetCore.Components.Web (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.