【问题标题】:Play Framework returning 404 with play.http.context and trailing slash播放框架返回 404,带有 play.http.context 和斜杠
【发布时间】:2020-02-11 08:25:03
【问题描述】:

我有一个提供 SPA 的 Play Framework 应用程序。路由文件包含以下路由:

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/ 时,Home 控制器中的 index 操作会按预期调用。

问题从我配置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 以使对两个 URL 的请求都以controllers.Home.index 成功?

【问题讨论】:

    标签: scala playframework


    【解决方案1】:

    我也在寻找相同的答案,但没有好的答案。对于 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

    【讨论】:

    • 它不适用于根 URL(这就是这个问题的意义所在) - 例如,我无法指定 //
    【解决方案2】:

    使用两个网址。带和不带斜杠。

    【讨论】:

    • 请提供正确答案。在这种情况下,正确的 URL 可能会有所帮助,
    • “使用两个网址”是什么意思?用在哪里?配置应该如何?路线应该是什么样子?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    相关资源
    最近更新 更多