Hosting a .NET 8 Core Web API with Swagger on an IIS server involves several steps, including installing required components, publishing your application, and configuring IIS. Here’s a detailed guide to help you through the process.
Prerequisites
- Windows Server with IIS installed.
- .NET 8 Hosting Bundle installed on the server.
- Visual Studio for development and publishing.
Step 1: Install Required Components
- Install IIS:
- Go to Control Panel > Programs > Turn Windows features on or off.
- Check the box for Internet Information Services (IIS) and install it.
- Install .NET 8 Hosting Bundle:
- Download and install the .NET 8 Hosting Bundle from the official Microsoft website. This includes the ASP.NET Core Runtime and necessary modules for IIS.
Step 2: Create Your Web API Project
- Open Visual Studio and create a new ASP.NET Core Web API project.
- Ensure that you enable Swagger support during project creation or add it later using NuGet packages (
Swashbuckle.AspNetCore
).
Step 3: Configure Your Project (.NET 8)
In .NET 8, the Program.cs
file replaces both the Main
method and the Startup.cs
. Here's how you should configure your Web API project with Swagger:
Example Program.cs
:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty; // Serve Swagger UI at the app's root
});
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
🔍 Key Points
-
WebApplication.CreateBuilder()
sets up the host, services, and configuration. -
app.UseSwagger()
enables the raw Swagger JSON endpoint. -
app.UseSwaggerUI()
serves the interactive documentation UI. -
RoutePrefix = string.Empty
makes Swagger UI accessible at the root URL (e.g.,http://localhost:5000
).
🧪 Testing Locally
After running your application, navigate to:
http://localhost:<port>/
You should see the Swagger UI page if everything is configured properly.
Step 4: Publish Your Application
- Right-click on your project in Visual Studio and select Publish.
- Choose a folder as the target location (e.g.,
C:\inetpub\wwwroot\MyWebAPI
). - Click Publish to generate the necessary files.
Step 5: Set Up IIS
- Open IIS Manager.
- Right-click on Sites and select Add Website.
- Fill out the following details:
- Site name: MyWebAPI
-
Physical path: Path to your published folder (e.g.,
C:\inetpub\wwwroot\MyWebAPI
). - Binding: Set appropriate bindings (e.g., HTTP on port 80).
- Click OK to create the site.
Step 6: Configure Application Pool
- In IIS Manager, go to Application Pools.
- Create a new application pool or use an existing one:
- Set the .NET CLR version to “No Managed Code” for ASP.NET Core applications.
- Ensure that the application pool identity has permissions to access your published folder.
Step 7: Test Your Application
- Open a web browser and navigate to your site’s URL (e.g.,
http://localhost/MyWebAPI
). - Access Swagger UI by navigating to
http://localhost/MyWebAPI/swagger
.
Troubleshooting Tips
- If you encounter a “404 Not Found” error when accessing Swagger, ensure that:
- The Swagger middleware is correctly configured in your
Startup.cs
. - The physical path in IIS points correctly to your published files.
- Check if all necessary IIS features are enabled, such as Static Content and Application Development features.
By following these steps, you should be able to successfully host your .NET 8 Core Web API with Swagger on an IIS server.
Citations:
[1] https://stackoverflow.com/questions/63058563/publishing-web-api-with-swagger-on-iis
[2] https://dev.to/jaydeep007/host-and-publish-net-core-6-web-api-application-on-iis-server-232p
[3] https://www.youtube.com/watch?v=8wiHS9naBs8
[4] https://www.codeproject.com/Articles/5387581/A-Full-featured-ASP-NET-Core-Data-Service-Web-API
[6] https://www.c-sharpcorner.com/article/hosting-asp-net-web-api-rest-service-on-iis-10/
[9] https://forums.servicestack.net/t/swagger-ui-not-showed-once-pubblished-in-iis/12111
Top comments (0)