Skip to content

Add url_for method to Router for named route URL generation#665

Merged
RobertoPrevato merged 7 commits intomainfrom
copilot/fix-issue-256-error-handling
Feb 22, 2026
Merged

Add url_for method to Router for named route URL generation#665
RobertoPrevato merged 7 commits intomainfrom
copilot/fix-issue-256-error-handling

Conversation

Copy link
Contributor

Copilot AI commented Feb 22, 2026

Router had no way to generate URLs by route name, making reverse URL lookup impossible without manually constructing paths.

Changes

  • RouteNotFound exception — raised when url_for is called with an unknown name; exported from blacksheep
  • Route.url(**params) — builds a URL from the route pattern by substituting named parameters
  • name parameter on all registration methods — optional name kwarg added to add(), add_get/post/...(), and all HTTP method decorators (get, post, etc.) on RouterBase
  • Router.url_for(name, **params) — resolves a named route (including across sub-routers) and returns its URL with parameters substituted; raises RouteNotFound if the name is not registered
  • Router._named_routes — internal dict[str, Route] populated at add() time, cleared on reset()

Usage

router = Router()

@router.get("/users/:username", name="user_detail")
def user_detail(): ...

router.apply_routes()

router.url_for("user_detail", username="johndoe")  # "/users/johndoe"

# Sub-routers are also searched
sub = Router()

@sub.get("/items/{int:item_id}", name="get_item")
def get_item(): ...

parent = Router(sub_routers=[sub])
parent.url_for("get_item", item_id="42")  # "/items/42"
Original prompt

Work on #256

Created from Copilot CLI via the copilot delegate command.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: RobertoPrevato <2576032+RobertoPrevato@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix error handling for issue 256 Add url_for method to Router for named route URL generation Feb 22, 2026
Copilot AI requested a review from RobertoPrevato February 22, 2026 07:19
@RobertoPrevato
Copy link
Member

The url_for method must be added to request to support base_path.

@RobertoPrevato RobertoPrevato deleted the copilot/fix-issue-256-error-handling branch February 22, 2026 07:36
@RobertoPrevato RobertoPrevato restored the copilot/fix-issue-256-error-handling branch February 22, 2026 07:49
@RobertoPrevato RobertoPrevato marked this pull request as ready for review February 22, 2026 21:03
@RobertoPrevato RobertoPrevato merged commit 57f907b into main Feb 22, 2026
31 of 32 checks passed
@RobertoPrevato RobertoPrevato deleted the copilot/fix-issue-256-error-handling branch February 22, 2026 21:07
RobertoPrevato added a commit that referenced this pull request Feb 23, 2026
Co-authored-by: GitHub Copilot <198982749+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants