Loading...
Loading...
Compare original and translation side by side
MapGroup()Endpoints/MapGroup()Endpoints/// src/Services/Sorcha.Wallet.Service/Endpoints/WalletEndpoints.cs
public static IEndpointRouteBuilder MapWalletEndpoints(this IEndpointRouteBuilder app)
{
var walletGroup = app.MapGroup("/api/v1/wallets")
.WithTags("Wallets")
.RequireAuthorization("CanManageWallets");
walletGroup.MapPost("/", CreateWallet)
.WithName("CreateWallet")
.WithSummary("Create a new wallet")
.WithDescription("Creates a new HD wallet with the specified algorithm");
walletGroup.MapGet("/{address}", GetWallet)
.WithName("GetWallet")
.WithSummary("Get wallet by address");
return app;
}// src/Services/Sorcha.Wallet.Service/Endpoints/WalletEndpoints.cs
public static IEndpointRouteBuilder MapWalletEndpoints(this IEndpointRouteBuilder app)
{
var walletGroup = app.MapGroup("/api/v1/wallets")
.WithTags("Wallets")
.RequireAuthorization("CanManageWallets");
walletGroup.MapPost("/", CreateWallet)
.WithName("CreateWallet")
.WithSummary("Create a new wallet")
.WithDescription("Creates a new HD wallet with the specified algorithm");
walletGroup.MapGet("/{address}", GetWallet)
.WithName("GetWallet")
.WithSummary("Get wallet by address");
return app;
}private static async Task<IResult> CreateWallet(
[FromBody] CreateWalletRequest request,
WalletManager walletManager,
HttpContext context,
ILogger<Program> logger,
CancellationToken cancellationToken = default)
{
try
{
var (wallet, mnemonic) = await walletManager.CreateWalletAsync(...);
return Results.Created($"/api/v1/wallets/{wallet.Address}", response);
}
catch (ArgumentException ex)
{
return Results.BadRequest(new ProblemDetails { Title = "Invalid Request", Detail = ex.Message });
}
}private static async Task<IResult> CreateWallet(
[FromBody] CreateWalletRequest request,
WalletManager walletManager,
HttpContext context,
ILogger<Program> logger,
CancellationToken cancellationToken = default)
{
try
{
var (wallet, mnemonic) = await walletManager.CreateWalletAsync(...);
return Results.Created($"/api/v1/wallets/{wallet.Address}", response);
}
catch (ArgumentException ex)
{
return Results.BadRequest(new ProblemDetails { Title = "Invalid Request", Detail = ex.Message });
}
}| Concept | Usage | Example |
|---|---|---|
| Route Groups | Shared config for related endpoints | |
| TypedResults | Type-safe return values | |
| OpenAPI Metadata | | Required on all endpoints |
| Authorization | | Apply to groups or individual endpoints |
| AllowAnonymous | Public endpoints | |
| Cache Output | Redis output caching | |
| 概念 | 用法 | 示例 |
|---|---|---|
| 路由分组(Route Groups) | 为相关端点共享配置 | |
| 类型化结果(TypedResults) | 类型安全的返回值 | |
| OpenAPI 元数据 | | 所有端点必须配置 |
| 授权(Authorization) | | 应用于分组或单个端点 |
| 允许匿名访问(AllowAnonymous) | 公开端点 | 登录路由上使用 |
| 输出缓存(Cache Output) | Redis输出缓存 | |
private static async Task<Results<Ok<TokenResponse>, UnauthorizedHttpResult, ValidationProblem>> Login(
LoginRequest request,
ITokenService tokenService)
{
if (string.IsNullOrWhiteSpace(request.Email))
return TypedResults.ValidationProblem(new Dictionary<string, string[]>
{
["email"] = ["Email is required"]
});
var token = await tokenService.LoginAsync(request);
if (token == null) return TypedResults.Unauthorized();
return TypedResults.Ok(token);
}private static async Task<Results<Ok<TokenResponse>, UnauthorizedHttpResult, ValidationProblem>> Login(
LoginRequest request,
ITokenService tokenService)
{
if (string.IsNullOrWhiteSpace(request.Email))
return TypedResults.ValidationProblem(new Dictionary<string, string[]>
{
["email"] = ["Email is required"]
});
var token = await tokenService.LoginAsync(request);
if (token == null) return TypedResults.Unauthorized();
return TypedResults.Ok(token);
}walletGroup.MapGet("/{address}/addresses", ListAddresses);
private static async Task<IResult> ListAddresses(
string address, // Route parameter
[FromQuery] string? type = null, // Optional filter
[FromQuery] bool? used = null, // Optional filter
[FromQuery] int page = 1, // Default pagination
[FromQuery] int pageSize = 50)walletGroup.MapGet("/{address}/addresses", ListAddresses);
private static async Task<IResult> ListAddresses(
string address, // 路由参数
[FromQuery] string? type = null, // 可选过滤器
[FromQuery] bool? used = null, // 可选过滤器
[FromQuery] int page = 1, // 默认分页页码
[FromQuery] int pageSize = 50) // 默认分页大小Fetch latest ASP.NET Core Minimal APIs documentation with Context7.
mcp__context7__resolve-library-id/websites/learn_microsoft_en-us_aspnet_coremcp__context7__query-docs/websites/learn_microsoft_en-us_aspnet_core使用Context7获取最新的ASP.NET Core Minimal APIs文档。
mcp__context7__resolve-library-id/websites/learn_microsoft_en-us_aspnet_coremcp__context7__query-docs/websites/learn_microsoft_en-us_aspnet_core