【问题标题】:What is the proper way to handle browser reload in Angular 10 ASP.NET Core 3.1 SPA application?在 Angular 10 ASP.NET Core 3.1 SPA 应用程序中处理浏览器重新加载的正确方法是什么?
【发布时间】:2020-06-26 05:02:51
【问题描述】:

我一直在使用来自 Microsoft 的 Content Reactor sample app。我已经升级了 Angular、.NET Standard、.NET Core 和各种 NuGet 包的版本。该应用现在使用带有端点路由的 Angular 10 和 ASP.NET Core 3.1。

我了解SpaServices/UseSpa() 已被弃用。

        app.UseSpa(spa =>
        {
            // To learn more about options for serving an Angular SPA from ASP.NET Core,
            // see https://go.microsoft.com/fwlink/?linkid=864501
        });

该站点正在开发/Visual Studio 中的本地主机上工作。但是,重新加载浏览器会导致 404 错误(例如,https://localhost:2297/home)。我知道这可能是由于该应用的 SPA 特性造成的。

鉴于 SpaServices 已弃用,支持浏览器按钮重新加载的正确方法是什么?

更新:该应用程序正在部署到 Microsoft Azure 应用程序服务。该应用使用 SignalR。

【问题讨论】:

  • (如何)你解决了这个问题?

标签: angular single-page-application asp.net-core-3.1


【解决方案1】:

为您的 IIS 服务器创建一个 web.config 文件,将 404 重定向到 index.html 以便 Angular 可以处理它们,例如:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <httpErrors>
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" prefixLanguageFilePath="" path="/" responseMode="ExecuteURL" />
    </httpErrors>
    <caching enabled="false" enableKernelCache="false" />
    <rewrite>
      <rules>
        <rule name="HTTPS Redirect" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

【讨论】:

    猜你喜欢
    • 2021-10-01
    • 2014-06-12
    • 2010-10-22
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    相关资源
    最近更新 更多