概念对照表
ASP.NET Core ↔ rust-webx
| ASP.NET Core | rust-webx | 说明 |
|---|---|---|
Program.cs / Startup.cs | Host::builder() | 应用入口 |
IServiceCollection | ServiceCollection | DI 注册 |
IServiceProvider | ServiceProvider | DI 解析 |
IHost | IHost / Host | 应用宿主 |
IHostedService | IHostedService | 后台服务 |
IRequest<T> (MediatR) | IRequest<T> | 请求标记 + 路由 |
IRequestHandler<T,R> | IRequestHandler<T,R> | 处理器 |
IMediator | IMediator | 中介者 |
IMediator.Send() | IMediator::send() | 调度请求 |
INotification | IEventRequest | 事件 |
INotificationHandler<T> | IEventHandler<T> | 事件处理器 |
IPipelineBehavior | IPipelineBehavior | 管道拦截 |
[HttpGet] | #[get] | GET 路由 |
[HttpPost] | #[post] | POST 路由 |
[Authorize] | #[authorize] | 授权(可多条规则,规则间 AND) |
EndpointDataSource | IEndpointCatalog | Host 构建、DI 注入的端点元数据 |
IStringLocalizer<T> | IStringLocalizer | 资源键查找,默认 culture 来自 Culture.Default |
AddLocalization() | use_localization(LocalizationOptions::new()) | 约定读取 locales/<culture>.json |
[Display] / XML docs | #[display] / #[description] / #[category] | 标题、描述、分组;回退到 /// |
[FromBody] | #[frombody](未标注字段在 POST/PUT/PATCH 上默认按 body 绑定) | Body 绑定 |
[FromRoute] | #[frompath](字段名与 {name} 一致;未标注字段也优先匹配路径) | 路径参数 |
[FromQuery] | #[fromquery](未标注字段在非 body 方法上按 query 绑定) | 查询参数 |
[FromHeader] | #[fromheader("X-Name")] | 请求头 |
User / ClaimsPrincipal | IdentityUser(#[fromclaim] 字段) | 统一身份:id/name/email/tenant/roles |
User.FindFirst("sub") | user.id 或 #[fromclaim("sub")] | 标准身份优先,特殊 claim 用键名 |
[FromForm] + IFormFile | 请求结构体字段声明为 FormFile | multipart 上传 |
List<IFormFile> | Vec<FormFile> | 多文件上传 |
IFormFile.CopyToAsync() | FormFile::save_as() / copy_to() | 落盘 |
FormOptions.MultipartBodyLengthLimit | Form.MaxFileSize / Form.MaxRequestSize | 上传大小限制 |
Results.File() / PhysicalFile() | FileResult::path() | 文件下载 |
File(Stream, ...) | FileResult::stream() | 流式下载 |
File(Stream, ...) + enableRangeProcessing | FileResult::seekable_stream() | 可 seek 流的断点续传 |
fileDownloadName | FileResult::download_name() | 下载文件名 |
enableRangeProcessing | FileResult::enable_range_processing() | 断点续传开关 |
IActionResult | IActionResult(Response = Box<dyn IActionResult>) | 完全掌控响应 |
Content(...) / Text(...) | ContentResult | 文本/字节响应 |
appsettings.json | appsettings.json | 配置文件 |
IConfiguration | AppOptions | 配置访问 |
ProblemDetails | ProblemDetails | RFC 9457 |
UseMiddleware<T>() | use_middleware(MiddlewareSpec::new::<T>()) | 中间件注册(spec 化) |
UseAuthentication() | use_authentication() | 认证 |
UseAuthorization() | 自动(#[authorize] 规则组在 endpoint 执行) | 授权 |
UseRequestLocalization() | 未实现请求级协商;用全局 Culture.Default | 国际化 |
UseCors() | use_cors() | CORS |
UseStaticFiles() | use_spa() | 静态文件 |
Controller | 无(Request 即端点) | 路由处理 |
ActionResult<T> | ActionResult<T>(= Result<T, Error> 别名) | 返回类型 |
NotFound() | Error::not_found(..) | 404 |
BadRequest() | Error::validation(..) | 400 |
IApplicationBuilder | HostBuilder(use_middleware(MiddlewareSpec)) | 应用配置 |
Axum ↔ rust-webx
| Axum | rust-webx | 说明 |
|---|---|---|
Router::new().route() | #[get] / #[post] | 路由定义 |
handler function | IRequestHandler | 处理器 |
State<T> | DI 注入 | 状态共享 |
Extension<T> | DI 注入 | 请求扩展 |
middleware::from_fn | IMiddleware | 中间件 |
Json<T> | ActionResult<T> 自动序列化 | JSON 响应 |
Multipart extractor | 请求结构体字段声明为 FormFile | multipart 上传 |
field.bytes().await | FormFile::read_bytes().await | 读取上传内容 |
ServeFile / ServeDir | FileResult::path() / use_spa() | 文件与静态资源 |
Body::from_stream | FileResult::stream() | 流式响应体 |
StatusCode | Error::status(code, ..) 或专用构造器(not_found…) | 状态码 |
AppState | ServiceCollection | 应用状态 |
axum::serve | Host::run() | 启动服务 |
tower::ServiceBuilder | MiddlewarePipeline | 中间件链 |
生态对照
| 需求 | ASP.NET Core | Axum | rust-webx |
|---|---|---|---|
| DI | 内置 | 手动/第三方 | rust-dix |
| ORM | EF Core | sqlx/sea-orm | 用户自选 |
| 认证 | Identity | 手动 | JWT 内置 |
| 文档 | Swagger | utoipa | OpenAPI 内置 |
| 配置 | appsettings | 手动/env | appsettings 内置 |
| 后台任务 | IHostedService | tokio::spawn | IHostedService |
| 文件上传 | IFormFile | Multipart | FormFile |
| 文件下载 | Results.File | ServeFile | FileResult::path |
小结
这张对照表是迁移时的速查手册。收藏本章,遇到概念疑问时随时查阅。
恭喜读完 rust-webx 开发者手册!
回到 目录 查阅具体章节,或运行 cargo run --manifest-path examples/docbit/Cargo.toml -p docbit-host 开始实践。