python
config_error
ai_generated
true
值错误: 具有相同路径的路由已存在。
ValueError: A route with the same path already exists.
ID: python/starlette-route-mount-conflict
80%修复率
84%置信度
0证据数
2024-03-22首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
在Starlette应用中重复注册相同路径的路由,导致冲突。
English
在Starlette应用中重复注册相同路径的路由,导致冲突。
解决方案
-
85% 成功率 删除旧路由再注册新路由
from starlette.routing import Route; routes = [route for route in app.routes if route.path != '/existing']; routes.append(Route('/existing', endpoint=new_handler)); app.router.routes = routes -
95% 成功率 使用不同的路径或添加版本前缀
app.add_route('/v2/existing', new_handler)
无效尝试
常见但无效的做法:
-
尝试覆盖路由而不删除旧路由
90% 失败
框架不允许重复注册。
-
使用不同的HTTP方法但路径相同
50% 失败
Starlette允许相同路径不同方法,但若方法也相同则冲突。