Publish and Host Asp.Net Web Api

In this blog post I am going to walk you through the steps involved in hosting an asp.net web api on IIS. We will be doing this on localhost. I am assuming you already have developed your api in either visual studio or visual studio code and have published it to some folder on your local machine. I published it to api folder on the desktop to make things easy. The contents of the published api looks like this:

Asp.net Web Api bin folder
Asp.net Web Api bin folder

Let’s launch Internet Information Services Manager. Click on start on type

Inetmgr

If no results come up this means you have to enable some windows features. Go to Control Panel > Programs and click on Turn Windows features on or off under Programs and Features. Alternatively you can click on Start and just type Turn Windows features on or off. Select Internet Information Services in the windows that pops up. It should look like this:

asp api window features
asp api window features

Click Ok and let it do its thing. Once done click on the Windows Start button and type:

Inetmgr

You are going to see IIS manager window popup. Under the connections pane (left side) right click and choose Add Website. Give your site a name, mine is grepsoft. You can call it whatever you want. Under Physical path chose the path where you published your api. I usually uncheck the ‘Start Website immediately’ since it’s an api and don’t want to launch a browser. Here are my settings:

asp api IIS add website
IIS add website settings

Hit Ok. You might be prompted with a warning message saying the binding is assigned to another site. It looks like this:

asp api IIS binding error
IIS binding error

It’s just telling you that port 80 is already being used. That’s fine, we can change it to something else. Hit No and change the port to 8080 (or whatever number you prefer). Your website should now be listed on the left side under Sites like this:

asp api IIS website
asp api IIS website

We need to start our site. Hit Start under the Actions pane on the right side.

asp api IIS start website
IIS start website

It’s very odd but if you are one of those lucky ones like I am, you might be welcomed with an error message that looks like this:

asp api IIS process use error

If this happens just restart your machine. Once restarted launch IIS Manager, select your site and then hit Start. To test our api I am going to use Postman or you can use fiddler. The path to our api is: 

http://localhost:8080/api/receipt

Hit Send in Postman. You will be greeted with 500 Internal Server error and a config error message saying: 

Cannot read configuration file due to insufficient permissions. 

If you click on Preview in Postman this is what you should see:

asp api IIS config file permission error

I have highlighted the important bit. Basically IIS is unable to read web.config file. Go back to IIS Manager and select your site. Then hit Stop. Then click Basic Settings under the Actions pane you should see this window:

asp api IIS edit site
IIS edit site

Click Test Settings and you should see a warning saying:

The server is configured to use pass-through authentication with a built-in account to access the specified physical path.

Looks like this:

asp api IIS test website
IIS authorization

We need to authorize IIS to read our Web.config file. Click Close. And then click Connect as. Select Specific user in the Connect as window and enter your credentials. It should be the logged in user for the machine. If you are on a domain make sure to enter that when specifying the user name like so: 

<domain>\username

Once you have done that things should be green like so:

asp api IIS test website success
IIS authorization success

Hit close and don’t forget to start your website. Now go to Postman and hit send again. This time there will be a different error:

Config Error: This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".

Here is a selfie:

asp api IIS section locked error
IIS section locked error

I was stuck on this one for a while and after doing some googling found this Stackoverflow article:

https://stackoverflow.com/questions/9794985/config-error-this-configuration-section-cannot-be-used-at-this-path

What they are saying is that we need to turn on Application Development Features. Click Start and then type Turn Windows features on or off and then under Internet Information Services > World Wide Web Services > Application Development Features select everything except CGI like this:

IIS application development features
IIS application development features

Click Ok and let it do it’s thing. Select your website and then click Restart under the actions pane in IIS Manager. Go back to Postman and hit Get again. This time there will be another error:

Could not load file or assembly 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. Access is denied.

Seems like more permissions. Go back to IIS Manager stop your website and this time click on Application Pools under the connections Pane.

asp api IIS application pools
IIS application pools

Click on your website and select Advanced Settings on the right side. Under the Process Model section look for the Identity row. It should be set to ApplicationPoolIdentiy. We need to switch this to the logged in user. Click on the Identity row and then click on the button with three dots (right side). Then choose Custom account under the Application Pool Identity window that pops up. Enter the credentials like you did before. Click Ok and restart your website. Go back to Postman and this time when you hit Send there will be another error! (just kidding). It should work, finally!

postman api success
api postman success

I know it’s a lot of info but i wanted to share the pain I went through in setting up an asp.net web api on IIS. I hope you liked it.

Leave a Reply

Your email address will not be published. Required fields are marked *