Fix generating urls for routes with optional parameters and static segments

Fixes #16658