Introduction
This article introduces how to publish and host an ASP.NET Web API on IIS server and how to call it. We will use same example used in the article
Getting Started With ASP.NET Web API. First of all we publish ASP.NET web API then will host on server.
Publish Web API from Visual Studio
To publish ASP.NET web API from Visual Studio, here are the steps,
- Build ASP.NET Web API project /solution in Release mode.
- Right click on Web API project and click “Publish” menu.
Figure 1: Publish ASP.NET Web API project
- Now open Publish Web pane and choose Profile tab from left tab. There is a “Select or import a public profile” in which you can either choose existing one or create new publish profile as in the following image,
Figure 2: Publish Profile for Web API
Now click “Ok” and move on Connection tab in Publish Web.
- Now we choose File System in publish method and choose desired location in Target Location which we will used to map IIS.
Figure 3: File System to publish web API
- Now we choose configuration mode Release as in the following image and click on Next button.
Figure 4: Configuration mode for Web API setting
There are some options such as Delete all existing files prior to publish which means it delete all existing files in target location publish folder and create new files.
- Now we get publish preview which shows publish path and profile name.
Figure 5: Publish Preview for Web API
Now click on publish button and all published files created at target location as in the following image,
Figure 6: Published files for web API
Host Web API on IIS Server
As we have published code so now we host this code on IIS step by step.
- Search inetmgr in search box and click on IIS manager icon.
- In the IIS manager, Right click on Sites under Connections pane as in the following image,
Figure 7: IIS Manager
- We have the following, now fill all the information which is required to host API on server.
Site name: Name of site
Application pool: .NETv4.5 (version of .net framework on which API run)
Physical path: Path of the published API code and map to directory which have Web.config file.
Figure 8: API host on IIS server.
Now click on OK and access API’s GET request method using the following URL:
http://localhost/api/home/getStudent