python
runtime_error
ai_generated
true
RuntimeError: 没有匹配路径 '/api/items/123' 的路由
RuntimeError: No route matches for path '/api/items/123'
ID: python/starlette-runtimeerror-no-route-match
80%修复率
82%置信度
0证据数
2024-05-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
请求的 URL 路径与 Starlette 应用程序中定义的任何路由不匹配。
English
The requested URL path does not match any defined route in the Starlette application.
解决方案
-
95% 成功率 Define a route with path parameter.
from starlette.applications import Starlette from starlette.responses import JSONResponse app = Starlette() @app.route('/api/items/{item_id}') async def get_item(request): return JSONResponse({'id': request.path_params['item_id']}) -
90% 成功率 Add a 404 handler for unmatched routes.
from starlette.responses import PlainTextResponse @app.exception_handler(404) async def not_found(request, exc): return PlainTextResponse('Not Found', status_code=404)
无效尝试
常见但无效的做法:
-
Adding a catch-all route without proper order.
50% 失败
Catch-all routes must be defined last; otherwise they override specific routes.
-
Using regular expression in route path incorrectly.
60% 失败
Starlette uses path parameters with curly braces, not regex. Misuse leads to no match.