Before we can access the API layer from our mobile application, we have to set up hosting. For this example, we are going to walkthrough setup for hosting locally.
Hosting locally does not require much work, but it will require an instance of Windows and Mac OSX running together. You can achieve this by simply running parallels, or using a Windows and Mac computer.
Our first step is to open Visual Studio from our Windows instance and click the run button:
When the project starts, it will automatically open up your default web browser and show the application.
Now that we have the backend running, how do we access the API?
If you are running via two separate computers, we should be able to simply access the API through the IP address of the computer. In order to find out the IP address of the computer, open up a new command prompt window and type in ipconfig
. This will display the IPv4 address that the computer has been assigned to on the current network.
To confirm we have this working, paste the following URL into a web browser on the Windows instance and see if we get results:
"localhost:{port}/api/StockItems"
Now we want to test this link on the Mac instance, but before we do, we have to change some settings in the applicationhost.config
file located in "C:Users[YourName]DocumentsIISExpressconfigapplicationhost.config"
.
If you are using Visual Studio 2015, it will be located in /{project folder}/.vs/config/applicationhost.config
.
If you haven't got Internet Information Services (IIS) switched on, follow these steps, to install IIS to serve static content:
<site name="Stocklist" id="43"> <application path="/" applicationPool="Clr4IntegratedAppPool"> <virtualDirectory path="/" physicalPath="C:UsersMichaelDocumentsStocklistStocklist" /> </application> <bindings> <binding protocol="http" bindingInformation="*: {PORT}:localhost" /> </bindings> </site>
<bindings>
section, we want to add another row:<binding protocol="http" bindingInformation="*:{PORT}:{IPv4 Address}" />
PORT
and IPv4 Address
from other computers.{IPv4}:{PORT}
with the IPv4 Address
and PORT
you are using:> netsh http add urlacl url=http://{IPv4}:{PORT}/ user=everyone
http.sys that it's OK to talk to this URL
. Next, run the following command: > netsh advfirewall firewall add rule name="IISExpressWeb"
dir=in protocol=tcp localport={PORT} profile=private
remoteip=localsubnet action=allow
localhost: {IPv4 address}:{port}/api/StockItems
.Config.resx
file in the Resources
folder of the Stocklist.Portable
project, and fill in these values:<data name="ApiAllItems" xml:space="preserve"> <value>http://{IPv4}:{PORT}/api/StockItems</value> </data> <data name="GetById" xml:space="preserve"> <value>http://{IPv4}:{PORT}/api/GetItemById</value> </data>
Now let's test our project on iOS and Android, and we should be able to see our StocklistPage
fill with items from our API controller.
18.220.124.177