How to Host ASP.NET Core Web API on IIS Server

Welcome, Dev! If you are looking for a step-by-step guide on hosting an ASP.NET Core Web API on an IIS server, then you are in the right place. In this article, we will walk you through the process of setting up and configuring your web API on IIS.

Prerequisites

Before we start, there are a few things that you need to have in order to successfully host your ASP.NET Core Web API on IIS. Here are some of the prerequisites:

  1. ASP.NET Core SDK installed on your system
  2. Internet Information Services (IIS) installed on your server
  3. ASP.NET Core Windows Server Hosting bundle installed on your server
  4. The .NET Core Runtime installed on your server

If you have all the prerequisites in place, then let’s move on to the next step.

Step 1: Create a Publish Profile

The first step in hosting your ASP.NET Core Web API on IIS is to create a publish profile. A publish profile is a set of settings that define how your web API will be published to the server. Here’s how you can create a publish profile:

  1. Open your ASP.NET Core Web API project in Visual Studio
  2. Right-click on your project in the Solution Explorer and select “Publish”
  3. Select “Folder” as the publish target and click “Next”
  4. Choose a target folder for the published files and click “Finish”
  5. Click on “Edit” to open the publish profile settings
  6. Choose “Framework-dependent” as the deployment mode
  7. Set the target runtime to the appropriate version of .NET Core installed on your server
  8. Save the publish profile

Step 2: Create an IIS Website

The next step is to create an IIS website that will host your ASP.NET Core Web API. Here’s how you can create an IIS website:

  1. Open Internet Information Services (IIS) Manager on your server
  2. Right-click on “Sites” and select “Add Website”
  3. Enter a site name and choose a physical path for the website
  4. Set the port number and IP address for the website
  5. Choose an application pool for the website
  6. Click “OK” to create the website

Step 3: Configure the IIS Website

Now that you have created an IIS website, you need to configure it to host your ASP.NET Core Web API. Here are the steps to configure your IIS website:

Step 3.1: Install ASP.NET Core Windows Server Hosting Bundle

The first thing you need to do is to install the ASP.NET Core Windows Server Hosting Bundle on your server. This bundle includes the .NET Core Runtime and enables IIS to host ASP.NET Core applications. Here’s how you can install the bundle:

  1. Download the ASP.NET Core Windows Server Hosting Bundle from the Microsoft website
  2. Run the installer on your server
  3. Follow the installation wizard to complete the installation

Step 3.2: Add a Web.config File to the Publish Folder

Next, you need to add a Web.config file to the publish folder. The Web.config file contains IIS settings that are required to host an ASP.NET Core Web API on IIS. Here’s how you can add a Web.config file:

  1. Navigate to the publish folder that you created in Step 1
  2. Create a new file named “Web.config”
  3. Paste the following code into the Web.config file:
READ ALSO  Creating a Self-Hosted Discord Server: A Comprehensive Guide for Devs
<?xml version=”1.0″ encoding=”utf-8″?>
<configuration>
<system.webServer>
<handlers>
<add name=”aspNetCore” path=”*” verb=”*” modules=”AspNetCoreModule” resourceType=”Unspecified” />
</handlers>
<aspNetCore processPath=”.\[your app].exe” arguments=”” stdoutLogEnabled=”false” stdoutLogFile=”.\logs\stdout” />
</system.webServer>
</configuration>

Note: Replace “[your app].exe” with the name of your ASP.NET Core Web API executable.

Step 3.3: Configure the IIS Website

Finally, you need to configure the IIS website to host your ASP.NET Core Web API. Here’s how you can configure the IIS website:

  1. Open Internet Information Services (IIS) Manager on your server
  2. Click on the website that you created in Step 2
  3. Double-click on “Handler Mappings”
  4. Click on “Add Module Mapping” in the Actions pane
  5. Enter a name for the module mapping
  6. Set the path to “*” and the verb to “*” (without quotes)
  7. Choose “AspNetCoreModule” as the module
  8. Enter the path to the ASP.NET Core Web API executable in the “Executable” field
  9. Set “Invoke handler only if request is mapped to” to “File”
  10. Click “OK” to save the module mapping
  11. Restart the website

FAQ

What is ASP.NET Core?

ASP.NET Core is an open-source web framework for building modern web applications. It is a cross-platform framework that can run on Windows, Linux, and macOS.

What is IIS?

Internet Information Services (IIS) is a web server software developed by Microsoft. It is used to host websites and web applications on Windows servers.

What is a Web API?

A web API (Application Programming Interface) is a set of protocols and tools for building web applications. It allows different applications to communicate with each other over the internet.

Do I need to install the .NET Core Runtime on my server?

Yes, you need to install the .NET Core Runtime on your server in order to host an ASP.NET Core Web API on IIS.

Can I host an ASP.NET Core Web API on Apache or Nginx?

Yes, you can host an ASP.NET Core Web API on Apache or Nginx using a reverse proxy. However, the steps may vary depending on your server environment.