scala - Play 框架返回 404 并带有 play.http.context 和尾部斜杠

标签 scala playframework

我有一个为 SPA 提供服务的 Play 框架应用程序。路由文件包含以下路由:

GET      /            controllers.Home.index
GET      /index.html  controllers.Home.index

# /api/* routes

GET      /*file       controllers.Assets.at(file)

controllers.Home.index 在执行 SSO 例程后提供 SPA 的索引。

当我在没有任何其他配置的情况下运行应用程序时,在访问 localhost:3000localhost:3000/ 时,index 操作将在 Home Controller 按预期调用。

当我配置play.http.context时,问题就开始了:

  • 当设置为/my-app时,对localhost:3000/my-app的请求成功;到 localhost:3000/my-app/ 返回 404。
  • 当设置为 /my-app/ 时,对 localhost:3000/my-app 的请求返回 404;到 localhost:3000/my-app/ 成功。

有没有办法配置 Play,以便通过 controllers.Home.index 对两个 URL 的请求都成功?

最佳答案

我也在寻找同样的答案,但没有好的答案。对于 Play/Akka,这是 2 个不同的 URL,

选项 1) 就是定义它2x

GET     /about                     controllers.HomeController.about
GET     /about/                     controllers.HomeController.about

选项 2) 将一个网址重定向到另一个网址

GET /a                              controllers.HomeController.about
GET /a/                             controllers.Default.redirect(to = "/a")

选项 3) 更高级的方法,但我没有测试过

https://doc.akka.io/docs/akka-http/current/routing-dsl/directives/path-directives/ignoreTrailingSlash.html

https://doc.akka.io/docs/akka-http/current/routing-dsl/directives/path-directives/redirectToNoTrailingSlashIfPresent.html

https://github.com/akka/akka-http/issues/880

关于scala - Play 框架返回 404 并带有 play.http.context 和尾部斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60164601/

相关文章:

java - Scala 有什么好处?

java - 如何将参数传递给 Play!框架 1.2.x?

scala - IntelliJ Play2 和 Scala 插件不兼容。使 IntelliJ 12 不再按预期工作

mysql - 在 scala 中使用 sqlContext.sql 进行子查询

scala - 在 Scala 中读取包含多行字符串的 CSV 文件

Scala 编译器错误 : package api does not have a member materializeWeakTypeTag

java - 在 sbt/play-framework 中包含/排除 Assets 文件夹/目录

java - 如何使用 Ebean 查询具有 onetomany 和 manytoone 关系的表

playframework - 是否可以将 html 页面用作 Play Framework 2 中的 View ?

scala - 如何使用我的相等比较器对 Spark DataFrame 进行 GroupBy?