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:
Let’s launch Internet Information Services Manager. Click on start on type
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:
Click Ok and let it do its thing. Once done click on the Windows Start button and type:
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:
Hit Ok. You might be prompted with a warning message saying the binding is assigned to another site. It looks like this:
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:
We need to start our site. Hit Start under the Actions pane on the right side.
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:
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:
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:
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:
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:
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:
Once you have done that things should be green like so:
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:
I was stuck on this one for a while and after doing some googling found this Stackoverflow article:
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:
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=126.96.36.199, 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.
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!
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.