Host and Publish .NET Core 6 Web API Application on IIS Server
Tags: dotnet, dotnetcore, iis, deployment
Published: 2023-08-15 05:28:21

We are going to discuss how to host and publish the.NET Core 6 Web API on IIS Server

## Introduction

Basically, Internet Information Service (IIS) is the flexible and general-purpose web server provided by Microsoft that will be run on Windows and used to serve requested files.

## Required Tools

- IIS Manager
- .NET Core SDK 6
- Visual Studio 2022

Let’s start,

**Step 1)**

Create a new .NET Core Web API Project

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqacje1nunnii63185uy.png)

**Step 2)**

Configure your new project

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erhgwzqv3jqgdysor1h2.png)

**Step 3)**

Provide additional information like .NET Framework Version, Open API, and HTTPS

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t7xme3znxvqq8knptvl3.png)

**Step 4)**

## Project Structure

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ff2zcy94vj2jei87rouw.png)

**Step 5)**

Create the Product Controller, add one endpoint inside that class and also check other files which are created by default

```
using Microsoft.AspNetCore.Mvc;
namespace WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductsController : ControllerBase
    {
        [HttpGet]
        [Route("list")]
        public IDictionary<int, string> Get()
        {
            IDictionary<int, string> list = new Dictionary<int, string>();
            list.Add(1, "IPhone");
            list.Add(2, "Laptop");
            list.Add(3, "Samsung TV");
            return list;
        }
    }
}
```

**_Program.cs file_**

```
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.Environment.IsProduction())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
```
Here also, you can see that we configure the HTTP request pipeline for both the development and Production environments.

**_WebAPI.csproj file_**

```
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>
</Project>
```

**_launchSetting.json file_**

```
{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:11254",
      "sslPort": 44315
    }
  },
  "profiles": {
    "WebAPI": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:7047;http://localhost:5047",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
```
**Step 6)**

Run your application and hit the API endpoint using Swagger UI


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7xvzxrtaa0viqrraswm.png)

**Step 7)**

Check if all IIS Services are running on your system properly or not. If not, then open the control panel, go to program and features, and click on Turn Windows Feature on or off

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9av025536ljisv5h7ms.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ioccbaxugeqskgpqhyug.png)

Apply the changes as I showed you in the above image, click on Apply, and then restart your computer

**Step 8)**

After restarting your computer, you will see IIS Manager in the search box. Open it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zz7gpa0m2n20vhh2mpz.png)

Now, check your running IIS using localhost in the browser

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u27rf482kw2lbn4ypx94.png)

**Step 9)**

Install ASP.NET Core 6.9 Runtime — Windows Hosting Bundle Installer

[Windows hosting bundle installer](https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/runtime-aspnetcore-6.0.6-windows-hosting-bundle-installer)

**Step 10)**

Now, we are going to publish our application for that, right-click on Web API Project and click on Publish


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/niunalz00bsvqh5fvxhh.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ry1nt9m6xbjpphuanqh7.png)

As you see it will take the default path, so change that path to c:\inetpub\wwwroot\publish after creating publish folder over there 

**Step 11)**

Here you can see all the configurations related to publishing like path and some environmental variables and later on click on Publish

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tytv7z8cakf9xbs46db4.png)

**Step 12)**

Open IIS Manager and create a new Web Application after right click on the sites

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqglel4le56dg0xyikcv.png)

**Step 13)**

Click on Web API and on the right side you can see the browse option so click on that and open the application inside the browser

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ifk0637rkqitknx4g01u.png)

**Step 14)**

In the browser when you hit the Web API endpoint using Swagger then will see the following list of products as an output

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kebm2m844spyhlclsga6.png)

## Conclusion

In this article, we discussed .NET Core 6 Web API hosting and publish related things using IIS and things related to that step-by-step. I hope you understand all things.

**Happy Coding!**