重定向
HTTP重定向
HTTP 重定向很容易。 内部、外部重定向均支持。
r.GET("/test", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "http://www.sogo.com/")
})
路由重定向
路由重定向,使用HandleContext
:
r.GET("/test", func(c *gin.Context) {
// 指定重定向的URL
c.Request.URL.Path = "/test2"
r.HandleContext(c)
})
r.GET("/test2", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"hello": "world"})
})
路由
普通路由
//GET请求
r.GET("/index", func(c *gin.Context) {...})
//POST请求
r.POST("/login", func(c *gin.Context) {...})
//任意
r.Any("/test", func(c *gin.Context) {...})
//没有匹配路由,则返回404页面
r.NoRoute(func(c *gin.Context) {
c.HTML(http.StatusNotFound, &#