Chapter 14: Mapping URLs to Pages
Razor Pages map page files to routes. FastAPI maps functions to URLs. To keep page routes maintainable, group them by feature.
src/recipevault/
web/
__init__.py
recipes.py
account.py
templates/
layout.html
recipes/
index.html
detail.html
edit.html
# web/recipes.py
router = APIRouter(prefix="/recipes", tags=["web-recipes"])
@router.get("")
def index(request: Request):
...
@router.get("/{recipe_id}")
def detail(recipe_id: int, request: Request):
...
The equivalent of Razor Page route conventions is your project convention. Write it down:
- Browser routes live under
web/. - JSON routes live under
api/. - Shared domain logic lives outside both.
- Templates mirror route modules.
This keeps your web and API surfaces from becoming tangled.