Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem implementing redirect with querystring parameters #54

Open
Jimmy99 opened this issue Jan 18, 2024 · 1 comment
Open

Problem implementing redirect with querystring parameters #54

Jimmy99 opened this issue Jan 18, 2024 · 1 comment

Comments

@Jimmy99
Copy link
Contributor

Jimmy99 commented Jan 18, 2024

I am struggling to implement a redirect with querystring parameters
It seems the controller.Redirect can receive optional parameters which are empty interfaces.
I've tried a few variations on this theme but the parameters never find their way into the URL.
In this example the route defaults to "itemselect" when I would expect "itemselect?id=13&name=aaa"
Anybody know how to implement this correctly?

var queryParam1 interface{} = "id=13"
var queryParam2 interface{} = "name=aaa"
return c.Redirect(ctx, "itemselect", queryParam1, queryParam2)

@mikestefanello
Copy link
Owner

Sorry for the confusion. The variadic argument in Redirect() are route parameters and not query parameters. It's easy to mix these up.

Route parameters, sometimes referred to as slugs, are for building the route dynamically.

For example, if you had a route to view a given user entity like:

router.GET("/user/:user_id", handler).Name = "user-profile"

To redirect to it, you would do:

ctr.Redirect(ctx, "user-profile", 123)

Which would redirect to /user/123.

The same route params are used in the Page.ToURL which can be called in the templates to generate route links. Using named parameters and this method is better (in my opinion) than hard-coding URLs all over your app.

As for supplying query params to Redirect(), that unfortunately is not currently supported but I don't see a reason why it cannot be added.

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

No branches or pull requests

2 participants