skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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
        });
    });
    

  2. 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.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search