Once you click OK a second dialog will show up and that one holds the option to start a WebAPI project.
What this gives you is an MVC project with an API controller. This behaves much like the normal controller that you’re used to from MVC but instead of returning a view it returns data in a JSON format (serializing happens with JSON.net by the way, not with the .net serializer). I’m not going to dive very deep in WebAPI, there are a lot of bloggers out there that know way more about this stuff than I do. We’ll be using the default GET method from the ValuesController.
This just returns a collection of two strings.
Next step is adding a Windows Phone 8 project to the solution (a simple basic project started from the normal template). Now we have two projects in one solution that both need to start up. We could launch the api without debugging and launch the WP8 app in debug mode or we can set them both to launch at debug by selecting multiple startup projects in the solution properties.
You can easily see that the settings have applied successfully if no project in the solution is bold anymore.
Okay, time to hook them up. We’ll need two things, the IP address of the pc running IIS Express and the port that the ASP.net project will use. Getting the IP address should be easy, just enter ipconfig in a command prompt. To find out the port, navigate to the properties of the ASP.net project.
In the Web tab of the properties you can see the project url for the website, that url contains the port that will be used.
Time for some coding, in the Windows Phone app, add a constant that holds the url to the website.
And the code to fetch the REST data
The callback will use Json.net (added through NuGet) to deserialize the values into a List<string> that is then set as the ItemsSource of a LongListSelector.
All in all pretty easy but this won’t work because IIS Express is bound to localhost only by default, we’ll need to change the config to allow external connections.
The configuration of IIS Express can be found in the applicationhost.config xml file found in %userprofile%\documents\IISExpress\config (just copy-paste this path into the Windows 8 start screen and press enter)
Open the XML file and search for the name of your ASP.net project. It should look something like this
Line 6 contains the binding, copy this line and paste it underneath, change localhost with the IP address of your pc, like this
Close the IIS Express server if it’s still running and run the project.
Now there’s a pretty big chance that you’ll see this.
Visual Studio 2012 needs to be started as administrator to allow IIS Express to create bindings for external connections, so if you get this error, close and restart Visual Studio 2012 as an administrator.
Run the project again and the Windows Phone application should be able to fetch the data from the API.
In this post I’ve talked about opening up your IIS Express development server to allow external connections. This is needed to allow the Windows Phone 8 emulator to connect to websites or APIs hosted locally. It’s basically two steps