DEV Community

Cover image for Step-by-Step Guide to Host .NET 8 Core Web API with Swagger in IIS
Ronak Munjapara
Ronak Munjapara Subscriber

Posted on • Edited on

Step-by-Step Guide to Host .NET 8 Core Web API with Swagger in IIS

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

  1. Install IIS:
  2. Go to Control Panel > Programs > Turn Windows features on or off.
  3. Check the box for Internet Information Services (IIS) and install it.
  4. Install .NET 8 Hosting Bundle:
  5. 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

  1. Open Visual Studio and create a new ASP.NET Core Web API project.
  2. 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();
Enter fullscreen mode Exit fullscreen mode

🔍 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>/
Enter fullscreen mode Exit fullscreen mode

You should see the Swagger UI page if everything is configured properly.


Step 4: Publish Your Application

  1. Right-click on your project in Visual Studio and select Publish.
  2. Choose a folder as the target location (e.g., C:\inetpub\wwwroot\MyWebAPI).
  3. Click Publish to generate the necessary files.

Step 5: Set Up IIS

  1. Open IIS Manager.
  2. Right-click on Sites and select Add Website.
  3. Fill out the following details:
  4. Site name: MyWebAPI
  5. Physical path: Path to your published folder (e.g., C:\inetpub\wwwroot\MyWebAPI).
  6. Binding: Set appropriate bindings (e.g., HTTP on port 80).
  7. Click OK to create the site.

Step 6: Configure Application Pool

  1. In IIS Manager, go to Application Pools.
  2. Create a new application pool or use an existing one:
  3. Set the .NET CLR version to “No Managed Code” for ASP.NET Core applications.
  4. Ensure that the application pool identity has permissions to access your published folder.

Step 7: Test Your Application

  1. Open a web browser and navigate to your site’s URL (e.g., http://localhost/MyWebAPI).
  2. 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

[5] https://www.c-sharpcorner.com/article/host-and-publish-net-core-6-web-api-application-on-iis-server2/

[6] https://www.c-sharpcorner.com/article/hosting-asp-net-web-api-rest-service-on-iis-10/

[7] https://blog.devops.dev/host-and-publish-net-core-6-web-api-application-on-iis-server-547e7777c9d?gi=ccee63d84a23

[8] https://learn.microsoft.com/en-us/aspnet/core/tutorials/publish-to-iis?view=aspnetcore-6.0&tabs=visual-studio

[9] https://forums.servicestack.net/t/swagger-ui-not-showed-once-pubblished-in-iis/12111

Top comments (0)