Select the product you need help with
Creating Simple Service with ASP.NET MVC Web API to be accessed by Windows Store and Windows Phone ApplicationArticle ID: 2778398 About Author:Collapse this table
On This PageASP.NET Web API
ASP.NET MVC 4 Web API is one of new feature that can be found in Visual Studio 2012. ASP.NET Web API is a framework that allows to make HTTP service that can be consumed by a web application on a web browser, a desktop application or mobile application. Web APIs are very useful as a source of data for the native app like Windows Store or Windows Phone application. Format output of Web API can be XML, JSON or the other. In this article will explain how to create a simple HTTP service to be consumed by Windows Store and Windows Phone application Create Blank Solution
Here are the steps to create an Blank Solution in Visual Studio 2012 :
Collapse this image The following image is a result that can be seen in the Solution Explorer. Collapse this image Create ASP.NET MVC Web API ProjectAfter Blank Solution is made, the next step is adding a Web API Project in the Solution. We can add ASP.NET MVC 4 Web API project into Solution by doing these steps :
Collapse this image Collapse this image The results can be seen in the Solution Explorer as shown below. Collapse this image Adding a ControllerNext we will make a simple Controller class to handle HTTP request. These are the steps to add a Controller :
Collapse this image Collapse this image Writing HelloWorldController Codes
In HelloWorldController.cs file will be added two methods as shown in the following example.
Calling Web API from the Web Browser
To call a Web API that has been made, we can do these following steps. On the menu select Debug > Start Debugging or we can press F5 key. The result can be seen as following picture. Collapse this image We can see the results of HelloWorldController.cs by accessing this url http://localhost:33011/api/HelloWorld/ Collapse this image The response from the request is JSON formatted, and this is the content of the file. The response from the request will be different if we use the Firefox as web browser. In Firefox, the response from the request is XML formatted. Collapse this image In the controller has two methods, to access the method Get (string id) can be done by accessing this url : Then the result can be seen in the picture below. Collapse this image Create Windows Store Project
In the above example has shown how to call a web API from a web browser. Now it will be shown how to create a Windows Store app to accessing Web APIs. The following are the steps to create a Windows Store Project :
Collapse this image Now we can see HelloWorldWS project in Solution Explorer. Collapse this image Calling Web API from Windows Store Application
Next we will work on MainPage.xaml. In MainPage.xaml will be added Button and TextBlock like in the picture below.
Collapse this image Here is a function of two items :
Here is the code in MainPage.xaml Collapse this image And here is the code in MainPage.xaml.cs. The following are the steps to run the Windows Store app with Visual Studio 2012 :
Collapse this image Create Windows Phone ProjectNext will make a simple Windows Phone 7.5 application to access Web API. To make Windows Phone 7.5 application, we will use the Visual Studio 2010 Express for Windows Phone, because Windows Phone project template is not available yet in Visual Studio 2012 when this article was written. These are the steps to create a Windows Phone app project :
Collapse this image Collapse this image we can see the results as shown below. Collapse this image Calling the Web API from Windows Phone
Next we will work on MainPage.xaml. In MainPage.xaml will be added Button and TextBlock as shown below.
Collapse this image Here are the contents of the file MainPage.xaml and MainPage.xaml.cs. Collapse this image What is Next?
This article has briefly introduce ASP.NET MVC 4 Web APIs, the next article will explain more about the Web API.
Community Solutions Content DisclaimerMICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.PropertiesArticle ID: 2778398 - Last Review: October 31, 2012 - Revision: 1.0
| Article Translations |



Back to the top








