routes - Beego-router:当我使用多级路由时,找不到正确的静态文件
问题描述
- go 版本 go1.14 darwin/amd64
- beego 1.12.1
- 当我的网址只有一级时,一切正常
beego.Router("/index", &controllers.HomeController{}, "get:Index")
- 但是当我的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,不知道为什么要添加路由字符串摘要。
解决方案
您应该在 html 头中使用基本标记。
<base href="http://yourhost/">
或者您应该包含具有绝对路径的资产。
<link rel="stylesheet" href="/static/style.css">
如果您不设置基本 url,浏览器会发送相对路径请求。
也不要忘记设置静态路径。
beego.SetStaticPath("/static","static")
推荐阅读
- javascript - 反应条件渲染
- qt - Qt Creator 自定义构建步骤无法复制文件
- android - Android Kotlin 测试。lateinit 属性 _db 尚未初始化
- javascript - 如何使用 jsPDF 保存大图像?
- c# - 等待密钥被释放
- java - JDK 1.6 的菱形语法替代方案
- javascript - 如何在单击事件的jquery函数中将文本框值作为参数传递
- opencv - How can I compile opencv from source at Win 10
- printing - Flutter创建汉字PDF文件并在打印机中打印文件
- php - Laravel 5.6 错误 MethodNotAllowedHttpException