Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

概念对照表

ASP.NET Core ↔ rust-webx

ASP.NET Corerust-webx说明
Program.cs / Startup.csHost::builder()应用入口
IServiceCollectionServiceCollectionDI 注册
IServiceProviderServiceProviderDI 解析
IHostIHost / Host应用宿主
IHostedServiceIHostedService后台服务
IRequest<T> (MediatR)IRequest<T>请求标记 + 路由
IRequestHandler<T,R>IRequestHandler<T,R>处理器
IMediatorIMediator中介者
IMediator.Send()IMediator::send()调度请求
INotificationIEventRequest事件
INotificationHandler<T>IEventHandler<T>事件处理器
IPipelineBehaviorIPipelineBehavior管道拦截
[HttpGet]#[get]GET 路由
[HttpPost]#[post]POST 路由
[Authorize]#[authorize]授权(可多条规则,规则间 AND)
EndpointDataSourceIEndpointCatalogHost 构建、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 / ClaimsPrincipalIdentityUser(#[fromclaim] 字段)统一身份:id/name/email/tenant/roles
User.FindFirst("sub")user.id 或 #[fromclaim("sub")]标准身份优先,特殊 claim 用键名
[FromForm] + IFormFile请求结构体字段声明为 FormFilemultipart 上传
List<IFormFile>Vec<FormFile>多文件上传
IFormFile.CopyToAsync()FormFile::save_as() / copy_to()落盘
FormOptions.MultipartBodyLengthLimitForm.MaxFileSize / Form.MaxRequestSize上传大小限制
Results.File() / PhysicalFile()FileResult::path()文件下载
File(Stream, ...)FileResult::stream()流式下载
File(Stream, ...) + enableRangeProcessingFileResult::seekable_stream()可 seek 流的断点续传
fileDownloadNameFileResult::download_name()下载文件名
enableRangeProcessingFileResult::enable_range_processing()断点续传开关
IActionResultIActionResult(Response = Box<dyn IActionResult>)完全掌控响应
Content(...) / Text(...)ContentResult文本/字节响应
appsettings.jsonappsettings.json配置文件
IConfigurationAppOptions配置访问
ProblemDetailsProblemDetailsRFC 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
IApplicationBuilderHostBuilder(use_middleware(MiddlewareSpec))应用配置

Axum ↔ rust-webx

Axumrust-webx说明
Router::new().route()#[get] / #[post]路由定义
handler functionIRequestHandler处理器
State<T>DI 注入状态共享
Extension<T>DI 注入请求扩展
middleware::from_fnIMiddleware中间件
Json<T>ActionResult<T> 自动序列化JSON 响应
Multipart extractor请求结构体字段声明为 FormFilemultipart 上传
field.bytes().awaitFormFile::read_bytes().await读取上传内容
ServeFile / ServeDirFileResult::path() / use_spa()文件与静态资源
Body::from_streamFileResult::stream()流式响应体
StatusCodeError::status(code, ..) 或专用构造器(not_found…)状态码
AppStateServiceCollection应用状态
axum::serveHost::run()启动服务
tower::ServiceBuilderMiddlewarePipeline中间件链

生态对照

需求ASP.NET CoreAxumrust-webx
DI内置手动/第三方rust-dix
ORMEF Coresqlx/sea-orm用户自选
认证Identity手动JWT 内置
文档SwaggerutoipaOpenAPI 内置
配置appsettings手动/envappsettings 内置
后台任务IHostedServicetokio::spawnIHostedService
文件上传IFormFileMultipartFormFile
文件下载Results.FileServeFileFileResult::path

小结

这张对照表是迁移时的速查手册。收藏本章,遇到概念疑问时随时查阅。


恭喜读完 rust-webx 开发者手册!

回到 目录 查阅具体章节,或运行 cargo run --manifest-path examples/docbit/Cargo.toml -p docbit-host 开始实践。