1. 所谓重定向,就是将原始的 URL 变为一个新的 URL,然后用新 URL 路由到的处理器中。<br />Gin中重定向很简单。内部、外部重定向均支持。最常用的,在 HandlerFunc 中调用 c.Redirect 方法即可。
    1. r.GET("/test", func(c *gin.Context) {
    2. c.Redirect(http.StatusMovedPermanently, "http://www.google.com/")
    3. })

    通过 POST 方法进行 HTTP 重定向

    1. r.POST("/test", func(c *gin.Context) {
    2. c.Redirect(http.StatusFound, "/foo")
    3. })
    1. 或者用路由重定向,使用 c.HandleContext
    1. r.GET("/test", func(c *gin.Context) {
    2. c.Request.URL.Path = "/test2"
    3. r.HandleContext(c)
    4. })
    5. r.GET("/test2", func(c *gin.Context) {
    6. c.JSON(200, gin.H{"hello": "world"})
    7. })