Search code examples
asp.net-coreazure-web-app-servicevisual-studio-2019azure-deployment

Asp.NET Core Web-API deployment issue to Azure AppService


I am getting a browser generic 404 "Page Not Found" errors after publishing to an Azure Application service for a C# ASP.NET Core Web-API project. My code works fine locally from VS2019 and IISExpress. I have even gone back to square one and created projects using the default project template (Web-Api), where I still get the same error. I am using the unmodified sample code VS generates for me, with no database or external dependencies!

The creation of the Azure App Service is fine, and the "Hey, App Service developers! Your app service is up and running..." page is displayed fine.

It is after the publish operation that things go south, and no page is returned to the browser, resulting in the 404 error.

What I have found is that if I "hide" the web.config file (by renaming it using the Azure App Service Editor), the "Hey, App Service developers..." page comes again comes up OK.

That must mean there is some run-time error happening, yeah?

The most I can pin it down to is via logging, and the error seems to be the "request path" and "not matching any endpoint", as per below, which is not helping me :(

2021-06-16 00:18:46.526 +00:00 [Debug] Microsoft.Extensions.Hosting.Internal.Host: Hosting started
2021-06-16 00:18:46.548 +00:00 [Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request starting HTTP/1.1 GET https://webapplication720210614173743.azurewebsites.net/  
2021-06-16 00:18:46.557 +00:00 [Debug] Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware: Wildcard detected, all requests with hosts will be allowed.
2021-06-16 00:18:46.558 +00:00 [Trace] Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware: All hosts are allowed.
2021-06-16 00:18:46.609 +00:00 [Debug] Microsoft.AspNetCore.Routing.Matching.DfaMatcher: No candidates found for the request path '/'
2021-06-16 00:18:46.615 +00:00 [Debug] Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware: Request did not match any endpoints
2021-06-16 00:18:46.637 +00:00 [Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request finished in 88.5694ms 404

The other strange thing is this does NOT happen if I use the "ASP.Net Core Web App" template.

How can I resolve this?


Solution

  • Create an api project, because there is no default page by default, so the phenomenon you encountered has appeared.

    Access to your api should be able to access your project normally.

    Solution

    Default, we can test our api in local, so in IsDevelopment it will generate swagger.json, it's why you get the issue. Try to add below code, you will get same result like local.

    1. change startup.cs file.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "EF_API v1"));
        }
        // default, no below code
        else {
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "EF_API v1"));
        }
        // add below code, when start with https://..../swagger
        var option = new RewriteOptions();
        option.AddRedirect("^$", "swagger");
        app.UseRewriter(option);
    
        app.UseHttpsRedirection();
    
        app.UseRouting();
    
        app.UseAuthorization();
    
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
    
    1. Gif

      enter image description here