首页 > 解决方案 > Beego-router:当我使用多级路由时,找不到正确的静态文件

问题描述

  1. go 版本 go1.14 darwin/amd64
  2. beego 1.12.1
  3. 当我的网址只有一级时,一切正常

beego.Router("/index", &controllers.HomeController{}, "get:Index")

  1. 但是当我的url超过一级时,程序找不到静态文件。

beego.Router("/summary/:all",&controllers.SummaryVersionController{})

这是控制器代码:

pathurl := this.Ctx.Input.Param(":all")
var s string = "'"
pathurl = fmt.Sprintf("%s%s%s",s,pathurl,s)
fmt.Println(pathurl)
table,err:= new(models.JobList).SummaryForVersionData(pathurl)
if err!=nil{
    logs.Error("SummaryVersionController => ", err)
    this.Abort("404")
}
this.Data["Contents"]=table
this.TplName = "600.html"

这是我尝试输入 URL“http://127.0.0.1:8080/summary/6.0.0”时的控制台输出

2020/07/11 19:22:23.554 [D] [server.go:2807]  |      127.0.0.1| 200 |   5.849898ms|   match| GET      /summary/6.0.0   r:/summary/:all
2020/07/11 19:22:23.581 [D] [server.go:2807]  |      127.0.0.1| 404 |    215.983µs| nomatch| GET      /summary/static/assets/css/app.min.css
2020/07/11 19:22:23.590 [D] [server.go:2807]  |      127.0.0.1| 404 |    460.945µs| nomatch| GET      /summary/static/assets/bundles/datatables/datatables.min.css
2020/07/11 19:22:23.590 [D] [server.go:2807]  |      127.0.0.1| 404 |    256.697µs| nomatch| GET      /summary/static/assets/bundles/datatables/DataTables-1.10.16/css/dataTables.bootstrap4.min.css
2020/07/11 19:22:23.593 [D] [server.go:2807]  |      127.0.0.1| 404 |    650.812µs| nomatch| GET      /summary/static/assets/css/style.css

实际上,程序应该在/static中找到静态文件,而不是/summary/static,不知道为什么要添加路由字符串摘要。

标签: routesbeego

解决方案


您应该在 html 头中使用基本标记。

  <base href="http://yourhost/">

或者您应该包含具有绝对路径的资产。

<link rel="stylesheet" href="/static/style.css">

如果您不设置基本 url,浏览器会发送相对路径请求。

也不要忘记设置静态路径。

beego.SetStaticPath("/static","static")

推荐阅读