英文:
Golang/gin: How to disable the "Found." link on HTTP redirect
问题
我正在使用gin框架,当用户已经登录时,我将重定向登录页面到主页。主页上显示了以下HTML元素。
<a href="/signin">Found</a> "."
如何在应用程序中禁用该元素?
这是我的应用程序代码。
func GetSignIn(c *gin.Context) {
// 从cookie中获取会话。检查电子邮件是否存在
// 如果存在则重定向到主页,否则显示登录页面。
session := sessions.Default(c)
if session.Get("email") != nil {
fmt.Println(session.Get("email"))
c.Redirect(302, "/")
}
c.HTML(200, "signin.html", "")
}
看起来像是Go语言的一部分
https://github.com/golang/go/blob/master/src/net/http/server.go#L2014
但我不确定如何禁用它。如果有人能指导我就好了。
谢谢!
英文:
I'm using gin framework and I'm redirecting signin to home page when the user is already signed in. It shows this html element on the home page.
<a href="/signin">Found</a> "."
How to disable that element from the app?
This is my app code.
func GetSignIn(c *gin.Context) {
// Get session from cookie. Check if email exists
// redirect to Home page else show signin page.
session := sessions.Default(c)
if session.Get("email") != nil {
fmt.Println(session.Get("email"))
c.Redirect(302, "/")
}
c.HTML(200, "signin.html", "")
}
It seems like, it's a Go thing
https://github.com/golang/go/blob/master/src/net/http/server.go#L2014
But I'm not sure, how to disable it. Would be great if someone guide me.
Thanks!
答案1
得分: 1
是的,看起来这是一个Go语言的问题,你无法禁用它。
我建议你编写自己的Redirect
结构,就像Gin中的render.Redirect
一样(你可以将它嵌入到你的新结构中,以避免一些复制粘贴):
https://github.com/gin-gonic/gin/blob/master/render/redirect.go
为新结构编写自己的Render
方法,基于net/http
中的代码,然后像这样使用重定向:
https://github.com/gin-gonic/gin/blob/32cab500ecc71d2975f5699c8a65c6debb29cfbe/context.go#L476
我认为这是绕过net/http
重定向行为的最简单方法。
英文:
Yeah, looks like it's a Go thing and you can't disable it.
I suggest you to write your own Redirect
structure like render.Redirect
from Gin (you can embed it in your new structure to avoid some copy pasting):
https://github.com/gin-gonic/gin/blob/master/render/redirect.go
Write your own Render
method for new structure based on code from net/http
and then use redirect like this:
https://github.com/gin-gonic/gin/blob/32cab500ecc71d2975f5699c8a65c6debb29cfbe/context.go#L476
I think it's the simplest way to bypass such net/http
redirect behavior.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论