简体   繁体   English

ASP.NET 核心带 SPA,如何处理 root 上的无效路由?

[英]ASP.NET Core with SPA, how to handle invalid routes on root?

Our ASP.NET Core with Single Page App as client, is hosted on Azure Web Service.我们的 ASP.NET 内核以单页应用程序作为客户端,托管在 Azure Web 服务上。 We noticed that all environments and deployment slots get an occasional POST action request on /index.html.我们注意到,所有环境和部署槽都会在 /index.html 上偶尔收到 POST 操作请求。 In the ASP.NET Core application, http requests to the root is routed to the SPA application files through configuring the SPA static files provider middleware:在 ASP.NET 核心应用程序中,通过配置 SPA static 文件提供程序中间件,将对根的 http 请求路由到 SPA 应用程序文件:

services.AddSpaStaticFiles(configuration => {
                configuration.RootPath = "ClientApp/dist/ClientApp";
});

When these POST actions are requested on /index.html, the application will throw an exception:当在 /index.html 上请求这些 POST 操作时,应用程序将抛出异常:

The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request. SPA 默认页面中间件无法返回默认页面“/index.html”,因为未找到它,并且没有其他中间件处理该请求。

In turn, the exception causes issues in our performance monitoring as the exceptions are not caught/handled anywhere.反过来,异常会导致我们的性能监控出现问题,因为异常不会在任何地方被捕获/处理。 Especially if this happens multiple times in a short time.特别是如果这种情况在短时间内发生多次。

Question: What can we configure to either immediately return 403 or similar response, or setup such that we at least catch the exception?问题:我们可以配置什么来立即返回 403 或类似的响应,或者设置为我们至少捕获异常?

The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request. SPA 默认页面中间件无法返回默认页面“/index.html”,因为未找到它,并且没有其他中间件处理该请求。

  • This problem arises when the wwwroot folder is not copied in build or publishing the project.当在构建或发布项目时未复制 wwwroot 文件夹时,会出现此问题。

  • In either cases, both commands don't copy the wwwroot folder.在任何一种情况下,这两个命令都不会复制 wwwroot 文件夹。

  • As a workaround, you can add this target to your project file:作为一种解决方法,您可以将此目标添加到您的项目文件中:

  <Target Name="AddGeneratedContentItems" BeforeTargets="AssignTargetPaths" DependsOnTargets="PrepareForPublish">
    <ItemGroup>
      <Content Include="wwwroot/**" CopyToPublishDirectory="PreserveNewest" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);@(Content)" />
    </ItemGroup>
  </Target>

What can we configure to either immediately return 403 or similar response, or setup such that we at least catch the exception?我们可以配置什么来立即返回 403 或类似的响应,或者设置为我们至少捕获异常?

  • Another reason can be, if your controller route attribute don't exactly match the request URL, then this type of error occurs.另一个原因可能是,如果您的 controller 路由属性与请求 URL 不完全匹配,则会发生此类错误。
  • Please refer the similar issue found in GitHub .请参考GitHub中发现的类似问题。

The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request. SPA 默认页面中间件无法返回默认页面“/index.html”,因为未找到它,并且没有其他中间件处理该请求。

  • This problem arises when the wwwroot folder is not copied in build or publishing the project.当在构建或发布项目时未复制 wwwroot 文件夹时,会出现此问题。

  • In either cases, both commands don't copy the wwwroot folder.在任何一种情况下,这两个命令都不会复制 wwwroot 文件夹。

  • As a workaround, you can add this target to your project file:作为一种解决方法,您可以将此目标添加到您的项目文件中:

  <Target Name="AddGeneratedContentItems" BeforeTargets="AssignTargetPaths" DependsOnTargets="PrepareForPublish">
    <ItemGroup>
      <Content Include="wwwroot/**" CopyToPublishDirectory="PreserveNewest" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);@(Content)" />
    </ItemGroup>
  </Target>

What can we configure to either immediately return 403 or similar response, or setup such that we at least catch the exception?我们可以配置什么来立即返回 403 或类似的响应,或者设置为我们至少捕获异常?

  • Another reason can be, if your controller route attribute don't exactly match the request URL, then this type of error occurs.另一个原因可能是,如果您的 controller 路由属性与请求 URL 不完全匹配,则会发生此类错误。
  • Please refer the similar issue found in GitHub .请参考GitHub中发现的类似问题。

I have found a solution in an issue case on GitHub.我在 GitHub 的问题案例中找到了解决方案。 This solution will call on the middleware only when the request meets the correct condition: when it's a GET request.此解决方案仅在请求满足正确条件时才会调用中间件:当它是 GET 请求时。

In the Configure method of the Startup class:在 Startup class 的 Configure 方法中:

app.UseWhen(context => HttpMethods.IsGet(context.Request.Method), builder =>
{
    builder.UseSpa(spa =>
    {
       // ... add any option you intend to use for the Spa middleware
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM