Redirect
Redirect allows to complete the execution of the current page or action by redirecting the client to another location.
All your methods set the header Location, send the corresponding HTTP status code and immediately complete the execution.
If you want to know when Response may be modified, consult the documentation of the Lifecycle.
Structure
ctx.Response.Redirect implement the following API:
Redirect
{
Permanent(string)
Temporary(string)
SeeOther(string)
} Methods
| Method | Code | Description |
|---|---|---|
Permanent(location) | 308 Permanent Redirect | It permanently redirects by preserving the HTTP method and the body of the request. |
Temporary(location) | 307 Temporary Redirect | It temporarily redirects by preserving the HTTP method and the body of the request. |
SeeOther(location) | 303 See Other | Redirects using a new request GET. |
Execution flow
When GOX runs any of the methods of Redirect, the current page or action ends immediately and the client is redirected to the indicated location.
if !validate.IsLogin(ctx) {
// La ejecución finaliza aquí.
ctx.Response.Redirect.SeeOther("/login")
// Este return no es necesario.
return
} Choose the right method
Use SeeOther() after processing forms or actions to be completed by showing another page.
ctx.Response.Redirect.SeeOther("/dashboard") Use Temporary() where the redirection is temporary and the HTTP method must be preserved.
ctx.Response.Redirect.Temporary("/maintenance") Use Permanent() where a route has changed permanently and client must use the new location in future requests.
ctx.Response.Redirect.Permanent("/docs") Best practices
- Use
SeeOther()to finalize forms and actions. - Use
Temporary()when the redirection is temporary. - Use
Permanent()where the change of location is final. - Configure
Responsebefore redirecting.