Project Mavericks
Project Mavericks

Reputation: 1

ASP.NET CORE 5.0 Web API works on IIS Express but gives 404 when hosted on IIS 10

I have a WEB APP and a Web API project within a single solution. Recently we added Swashbuckle and Swagger UI to the API Project. Now, The Web API works fine when run form within VS (debug / without debugging) using IIS Express, Swagger.json is accessbile, Swagger UI is served on https://localhost:/API/docs/index.html and on trying it out https://localhost:/API/docs/, returns valid output.

However, after publishing and deploying onto IIS Site, it returns a 404 error. The Swagger.json is formed, the Swagger UI is served correctly on https://domain:/API/docs/index.html but when trying out https://domain:/API/docs/ responds back with 404. (DNS is mapped correctly).

IIS 10.0 Detailed Error - 404.0 - Not Found

Detailed Error Information: 
Module        IIS Web Core 
Notification MapRequestHandler 
Handler       StaticFile 
Error Code   0x80070002 
Requested URL https://localhost:<port>/Rejected-By-UrlScan?~/API/docs/<command> 
Physical Path <path>iis\abc\Rejected-By-UrlScan 
Logon Method Anonymous 
Logon User      Anonymous

Publishing through CLI successful

dotnet publish -c release -r win-x64 --self-contained false

Startup.cs

  public void ConfigureServices(IServiceCollection services)
            {
            
 
 
 
 
 
 
 
 services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme)
            .AddCertificate();

            services.Configure<ApplicationSetting>(Configuration.GetSection("ApplicationSettings"));
            services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
                                                                    .AllowAnyMethod()
                                                                     .AllowAnyHeader()));


            services.AddControllers()
                    .ConfigureApiBehaviorOptions(options =>
                    {
                        options.SuppressModelStateInvalidFilter = true;
                    });


            services.AddMvcCore().SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
                    .AddNewtonsoftJson(options =>
                    {
                        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                    });

            services.AddMvcCore()
                    .AddApiExplorer();

            abcDBContext.ConnectionString = Configuration.GetConnectionString("abcDBContext");

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = API",
                    Description = "Web API",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact = new OpenApiContact
                    {
                        Name = "ABC Support",
                        Email = string.Empty,
                        Url = new Uri("https://example.com/"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url = new Uri("https://example.com/license"),
                    }
                });

                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

            DependencyInjections.Dependency(services,Configuration);
             
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            if (env.IsProduction() || env.IsStaging())
            {
                app.UseExceptionHandler("/Error/index.html");
            }

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger(c =>
            {
                c.RouteTemplate = "docs/{documentName}/swagger.json";
            });

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.RoutePrefix = "docs"; 
                c.SwaggerEndpoint("v1/swagger.json", "v1");

                // custom CSS
                c.InjectStylesheet("/swagger-ui/custom.css");
            });

            app.Use(async (ctx, next) =>
            {
                await next();
                if (ctx.Response.StatusCode == 204)
                {
                    ctx.Response.ContentLength = 0;
                }
            });


            app.UseCors(builder =>
            builder.WithOrigins(Configuration["ApplicationSettings:Client_URL"].ToString())
            .AllowAnyOrigin()
            .AllowAnyHeader()
            .AllowAnyMethod()
            );

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();
            app.UseAuthentication();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseCors();

        }

LaunchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:2618",
      "sslPort": 44311
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "docs",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Web.API": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "docs",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

OS : Windows 10 latest version x64

IIS Site (using same application pool)

Application Pool Details

IIS Site

Upvotes: 0

Views: 2043

Answers (1)

Project Mavericks
Project Mavericks

Reputation: 1

I solved this by uninstalling URLScan 3.1, based on the (Link) shared by Lex Li and from here

Upvotes: 0

Related Questions