.NET Core部署至IIS后无法访问Swagger的问题求助
Let's troubleshoot your Swagger 404 issue step by step—this is a common problem when moving from local development to IIS deployment, and there are a few key areas to check:
1. Ensure Swagger is Enabled for the Deployment Environment
By default, Swagger is only enabled in the Development environment. When deployed to IIS, your app is likely running in Production mode, which would skip the Swagger middleware entirely.
Update your Startup.cs (or Program.cs if using top-level statements) to either:
- Enable Swagger in all environments (great for testing/prototyping):
// Remove the if (env.IsDevelopment()) check that wraps Swagger setup app.UseSwagger(); app.UseSwaggerUI(c => { // We'll adjust this path in the next step c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestWebApi v1"); }); - Or explicitly include your target IIS environment (if you've configured a custom environment like
Staging):if (env.IsDevelopment() || env.IsEnvironment("Staging") || env.IsProduction()) { app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestWebApi v1"); }); }
2. Correct the Swagger Endpoint Path for IIS Sub-Apps
If your Web API is deployed as an IIS sub-application (e.g., http://your-server/TestWebApi), the relative path ../swagger/v1/swagger.json won't resolve correctly. You need to use a root-relative path that includes the sub-app name:
For a sub-app named TestWebApi, update the endpoint to:
c.SwaggerEndpoint("/TestWebApi/swagger/v1/swagger.json", "TestWebApi v1");
If it's the root application (directly under http://your-server/), use the simple root-relative path:
c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestWebApi v1");
To verify the JSON file exists, try accessing it directly in your browser (e.g., http://your-server/TestWebApi/swagger/v1/swagger.json). If this returns 404, the issue is almost certainly that UseSwagger() isn't being enabled (go back to step 1).
3. Verify Middleware Order
Middleware order is critical in .NET Core. Ensure your pipeline follows this sequence in Startup.cs:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseHttpsRedirection(); app.UseStaticFiles(); // Must come before UseSwaggerUI (serves Swagger UI's static assets) app.UseRouting(); app.UseAuthorization(); // Swagger middleware comes after routing/authorization but before endpoint mapping app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/TestWebApi/swagger/v1/swagger.json", "TestWebApi v1"); }); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
4. Check IIS Application Pool Configuration
Make sure your IIS application pool is set up for .NET Core 5.0:
- Set .NET CLR Version to
No Managed Code(since .NET Core apps use their own runtime, not IIS's managed pipeline). - Set Managed Pipeline Mode to
Integrated. - Ensure the pool's identity has read permissions to your app's deployment directory.
5. Confirm Swagger JSON Generation
Your current AddSwaggerGen configuration looks solid, but if you've added XML documentation (a common Swagger enhancement), ensure the XML file is being copied to the output directory:
- In your project properties, under Build > Output, check "XML documentation file".
- Add this to
AddSwaggerGento include the comments (if applicable):var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath);
Try these steps one by one—most likely the issue is either an environment restriction blocking Swagger, or an incorrect endpoint path for your IIS setup.
内容的提问来源于stack exchange,提问作者sachin kulkarni

