简体   繁体   中英

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. We noticed that all environments and deployment slots get an occasional POST action request on /index.html. 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:

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

When these POST actions are requested on /index.html, the application will throw an exception:

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.

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?

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.

  • This problem arises when the wwwroot folder is not copied in build or publishing the project.

  • In either cases, both commands don't copy the wwwroot folder.

  • 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?

  • Another reason can be, if your controller route attribute don't exactly match the request URL, then this type of error occurs.
  • Please refer the similar issue found in 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.

  • This problem arises when the wwwroot folder is not copied in build or publishing the project.

  • In either cases, both commands don't copy the wwwroot folder.

  • 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?

  • Another reason can be, if your controller route attribute don't exactly match the request URL, then this type of error occurs.
  • Please refer the similar issue found in GitHub .

I have found a solution in an issue case on GitHub. This solution will call on the middleware only when the request meets the correct condition: when it's a GET request.

In the Configure method of the Startup class:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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