The Wayback Machine - https://web.archive.org/web/20210716143836/https://github.com/dotnet/aspnetcore/issues/34420
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST operation in Production environment: System.InvalidOperationException: The SPA default page middleware could not return the default page '/index.html' #34420

Open
James764 opened this issue Jul 16, 2021 · 0 comments

Comments

@James764
Copy link

@James764 James764 commented Jul 16, 2021

Describe the bug

I have a simple ASP Net core 5 application with Angular ( the template was generated with Microsoft Visual Studio Professional 2019 Version 16.8.3 )

In the development environment if I try to do a POST operation to a wrong path I'm getting 404, but in production I'm getting 500 with the exception System.InvalidOperationException: The SPA default page middleware could not return the default page '/index.html'

This behavior is bothering me because I would prefer 404 instead 500. My application is hosted in Azure and from time to time I see many requests like: POST /Index.html, OPTIONS /index.html, DEBUG /index.html.. and many other weird request methods. I don't know the source of these request, seems to be an automated tool, but this is generating a lot of logs with 500 code.

My temporary solution is to put a custom middleware just before app.UseSpa(..), but I believe this is a bug in ASP Net core, and the correct behavior is to return 404, as it is working in development environment.
Appreciate if you can give me some advise on my implementation that tries to avoid this error.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ......
    app.Use(async (context, next) =>
    {
        //Redirect to root request to /index.html ( except the GET request ) 
        //Do not generate logs for requests originating from automated tools: POST /Index.html, OPTIONS /index.html, DEBUG /index.htm, etc.. 
        if (!HttpMethods.IsGet(context.Request?.Method)
            && context.Request?.Path.Value.ToLower() == "/index.html")
        {
            context.Response.Redirect("/");
            return;
        }

        try
        {
            await next();
        }
        catch (InvalidOperationException spaException)
        {
            if (spaException.Message.StartsWith("The SPA default page middleware could not return the default page"))
            {
                context.Response.StatusCode = StatusCodes.Status404NotFound;
                return;
            }
            throw;
        }
    });

    app.UseSpa(...);
    .....
}

To Reproduce

  1. Create a new ASP Net core 5 application with Angular in Visual Studio
  2. Run the application ( debug or release mode )
  3. Make a post request with postman to https://localhost:port/WrongPath - this will return 404
  4. Stop the application, switch to release mode and publish the application with folder profile. Keep the default settings - in my case the folder was "MyAppName\bin\Release\net5.0\publish". This action will build the angular frontend also in MyAppName\ClientApp\dist,
  5. In solution explorer, go to project properties, then go to Debug section - environment variables, and change environment from Development to Production
  6. Start the application and make the same post request to https://localhost:port/WrongPath - this time you will get 500 error. Note that this is not a problem with the angular build, navigating to existing path is working fine.

Exceptions (if any)

System.InvalidOperationException: 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.
Your application is running in Production mode, so make sure it has been published, or that you have built your SPA manually. Alternatively you may wish to switch to the Development environment.

at Microsoft.AspNetCore.SpaServices.SpaDefaultPageMiddleware.<>c__DisplayClass0_0.b__1(HttpContext context, Func1 next) at Microsoft.AspNetCore.Builder.UseExtensions.<>c__DisplayClass0_1.<Use>b__1(HttpContext context) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Builder.UseExtensions.<>c__DisplayClass0_2.<Use>b__2() at Microsoft.AspNetCore.SpaServices.SpaDefaultPageMiddleware.<>c__DisplayClass0_0.<Attach>b__0(HttpContext context, Func1 next)
at Microsoft.AspNetCore.Builder.UseExtensions.<>c__DisplayClass0_1.b__1(HttpContext context)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.HttpsPolicy.HstsMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()
The thread 0x7a20 has exited with code 0 (0x0).

Further technical details

.NET SDK (reflecting any global.json):
Version: 5.0.101
Commit: d05174dc5a

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19042
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.101\

Host (useful for support):
Version: 5.0.1
Commit: b02e13abab

.NET SDKs installed:
3.0.101 [C:\Program Files\dotnet\sdk]
3.1.100 [C:\Program Files\dotnet\sdk]
5.0.101 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant