beego设置静态路由的几种方法
main.go里:
beego.SetStaticPath("tests_url", "tests") //第一个是访问的路径,第二个是根下目录
方法二:
package mainimport ( _ "./routers" "github.com/astaxie/beego" "github.com/astaxie/beego/context" //注意:是这个context "log" "net/http" "strings")func ignoreStaticPath() { //过滤器 beego.InsertFilter("/", beego.BeforeRouter, TransparentStatic) beego.InsertFilter("/*", beego.BeforeRouter, TransparentStatic)}func TransparentStatic(ctx *context.Context){ orpath := ctx.Request.URL.Path log.Println(orpath) //如果url中有api,则取消静态路由重定向 if strings.Index(orpath, "api") >= 0 { return } http.ServeFile(ctx.ResponseWriter, ctx.Request, "static/html/" + orpath)}func main() { ignoreStaticPath() beego.Run()}
注意:导入的是github.com/astaxie/beego/context,而不是context
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。