【问题标题】:Azure App Service Static Website routes.json doesn't workAzure 应用服务静态网站 routes.json 不起作用
【发布时间】:2020-10-18 19:24:32
【问题描述】:

我使用routes.json 将我的静态网站(React 应用)部署到 Azure 应用服务,以启用 HTML5 路由回退。我的路由 json 位于 /public 中,可在 URL https://mywebsite.azurewebsites.net/routes.json 下找到。

{
  "routes": [
    {
      "route": "/*",
      "serve": "/index.html",
      "statusCode": 200
    }
  ]
}

重定向不起作用。我做错了什么?

【问题讨论】:

  • 我已经更新了我的答案。从您的 url 地址,我确定您使用的服务不是static web apps。如果你创建static web apps,你的url地址应该是`yourstaticwebapp.azurestaticapps.net`。
  • @JasonPan 感谢所有这些截图和解释。我还没有在 github 上尝试过,但我确信它可以工作。是的,绝对的,我会接受你的回答!
  • routes.json 文件现已弃用,但路由、覆盖和身份验证规则现在都在 staticwebapp.config.json file 中定义。

标签: reactjs azure azure-web-app-service azure-static-website-hosting azure-static-website-routing


【解决方案1】:

最新

我认为您混淆了Web AppStatic Web App 这两个服务。 routes.json文件只在Static Web App生效。

根据 React 文档,执行以下命令创建 my-app 项目。

  1. npx create-react-app my-app
  2. cd my-app
  3. npm run start

然后在公用文件夹中创建index2.html

在 github 中创建一个名为 reacttest 的新存储库。

在my-app目录下,打开cmd,执行如下命令,将代码上传到github。

  1. git init
  2. git add.
  3. git commit -m'init'
  4. git remote add origin https://github.com/yourname/reacttest.git
  5. git push -u origin master

按照官方文档创建Azure Static Web Apps Preview。查看 github 中Action 的发布状态。发布后可以看到默认页面。

这时候直接在github的public文件夹中添加routes.json文件即可。继续查看 Action 中的发布状态。发布完成后,可以看到路由已经生效了。

当我们访问默认主页时,可以看到index2.html的内容。

如果想看到index.html,我们可以在url中添加/index.html

【讨论】:

  • 如果你打开yourwebsite.azurewebsites.net/non-existing-route,你还会看到index2.html的内容吗?它会回退到 index2
  • @Andrei 你可以去看看。
  • @Andrei 这是你想要的效果吗?
  • @Andrei 我不明白你为什么要添加/non-existing-route。关于这种用法的哪篇文章,能不能给我分享一下链接,谢谢。
猜你喜欢
  • 2020-12-10
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 2016-08-31
  • 2019-10-05
  • 1970-01-01
  • 2021-12-16
相关资源
最近更新 更多