Host ASP.NET Web API in IIS using Visual Studio Publish method

This article will help you to host your ASP.NET Web API in IIS using Visual Studio Publish method. You will use File System publish method for it.


Hosting ASP.NET Web API

You can host ASP.NET Web API as

  1. Self Hosted: You can host ASP.NET Web API using HttpSelfHostServer in Console, Windows forms, ASP.NET Web Application, ASP.NET MVC application or in windows services.
  2. IIS hosting: You can host your ASP.NET Web API in IIS and use as web server to make it available to clients through direct HTTP calls.

Benefits of Hosting ASP.NET Web API in IIS

It works like other IIS application so it will have benefits like load balancing, remote deployments. It provides process activation and recycling capabilities which helps to increase reliability of application. It is easy and safer to deploy your Web API appication to IIS. For detail description of ASP.NET Web API concepts see Getting Started with ASP.NET Web API


Steps for Hosting ASP.NET Web API in IIS


Create ASP.NET Web API

You can Create ASP.NET Web API application. The article linked with this create Empty ASP.NET Web API solution and give you RESTful CRUD methods.


Publish ASP.NET Web API

Open CustomerApp application created in previous step.

Right click on CustomerApp project and select Publish. You will see Publish Web wizard.

In Profile tab -> Select New Profile from DropDownList and give profile name as CustomerAppProfile.

Next is Connection tab -> Select File System from Publish Method DropDownList. Give path as Target Location where you would like to publish API.

publish asp.net web api in iis

Click Next button and select Release from Configuration DropDownList and finally click Publish button.


Publish ASP.NET Web API

Open CustomerApp application created in previous step.

Right click on CustomerApp project and select Publish. You will see Publish Web wizard.

In Profile tab -> Select New Profile from DropDownList and give profile name as CustomerAppProfile.

Next is Connection tab -> Select File System from Publish Method DropDownList. Give path as Target Location where you would like to publish API.

publish asp.net web api in iis

Click Next button and select Release from Configuration DropDownList and finally click Publish button.


Host CustomerApp in IIS

Open IIS manager by clicking Windows start -> Run -> enter inetmgr -> click ok

If IIS is not installed on your machine click here to install.

Go to IIS manager and right click on sites and select Add Web site.

Add new web site to IIS

Enter details as shown in below screenshot

  • Enter site name as CustomerApp
  • Change the application pool to ASP.net V4.0
  • Select the physical path of folder which contains published ASP.NET Web API. I entered as C:\CustomerApp
  • Enter port number on you wish to host API. I use port as 9810.

ASP.NET Web API application in IIS


Browse ASP.NET Web API through IIS

Your Web API are now hosted in IIS. You should be able to browse it through IIS or browser.

From IIS manager -> Select Sites -> right click on CustomerApp (created in previous step) -> Select Manage Web Site -> and select Browse.

It will redirect to http://localhost:9810. As corresponding routing url is not mapped in CustomerApp, it will throw an IIS error.

Try below url for which route mapping has done in Web API.

    
    http://localhost:9810/api/customer/getallcustomersr

    http://localhost:9810/api/customer/getcustomer/anatr
        
            

You will see below output.

ASP.NET Web API application in IIS


Accessing Database under IIS APPPOOL\\ASP.NET v4.0

As we are using ASP.NET v4.0 App Pool make sure IIS APPPOOL\\ASP.NET v4.0 is added to your Database Server -> Security -> Logins.

If it's not there go ahead and add new login with name IIS APPPOOL\\ASP.NET v4.0 or else it will throw run time error exception Login failed for user 'IIS APPPOOL\\ASP.NET v4.0'

Speak your mind :
Leave a comment for this article on dotnetbloogers.com
User profile picture on dotnetmentors.com

by Abhishek Pandey at 4/10/2015 5:47:00 AM
I have Created this API and successfully hosted on server and do sanity with Poster, It worked. Now I want to use this API within my Web Form Project and want to use it as virtual directory.. how can i achieve this.
post comment on dotnetmentors.com
User profile picture on dotnetmentors.com

by Laxmikant at 4/10/2015 6:10:00 AM
below article will help you to achieve your requirement http://dotnetmentors.com/web-api/rest-based-crud-operations-with-asp-net-web-api.aspx as you are hosting web api separately from your web form application you will have to enable CORS in web api and in web form application, you can use WebClient to call web api.
post comment on dotnetmentors.com
User profile picture on dotnetmentors.com

by Nissim at 12/23/2016 9:52:00 PM
Nope. No joy. I'm getting the following 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".
post comment on dotnetmentors.com
User profile picture on dotnetmentors.com

by Nissim at 12/23/2016 9:53:00 PM
Does not work. I get following 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".
post comment on dotnetmentors.com
User profile picture on dotnetmentors.com

by Nissim at 12/23/2016 9:54:00 PM
Does not work. I get following 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".
post comment on dotnetmentors.com
User profile picture on dotnetmentors.com

by Laxmikant at 12/27/2016 12:29:00 AM
@Nissim, you need to execute two commands from and elevated command prompt 1. %windir%/system32/inetsrv/appcmd unlock config /section:anonymousAuthentication 2. %windir%/system32/inetsrv/appcmd unlock config -section:windowsAuthentication
post comment on dotnetmentors.com