Channels ▼


Access Data with REST in Windows 8 Apps

The GroupsController class inherits from System.Wep.Http.ApiController. Because you've chosen the Empty API controller template, there aren't methods defined for the class. A complete REST service would require you to implement the following methods in a simple entity with a numeric ID and a string value:

C# Method definition REST call sample

IEnumerable<string> Get()

GET api/names

Group Get(int id)

GET api/names/10302030

void Post([FromBody]string value)

POST api/names

void Put(string id, [FromBody]string value)

PUT api/names/10302031

void Delete(int id)

DELETE api/names/10302032

Because the goal is to retrieve all the groups with their items from the Windows Store app, I focus just on the IEnumerable<Group> Get() method. The following code creates an array of Group with six groups. Only one group has two items, just to keep the example simple. You can add more items to other groups in order to check how the Grid app works in detail.

namespace DrDobbsWebAPI.Controllers
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Http;

    using DrDobbsApp.Entities;

    public class GroupsController : ApiController
        private readonly Group[] _groups = new[]
            new Group
                Id = "1",
                Title = "Cloud",
                Subtitle = "Cloud Developer",
                Description = "Cloud feature articles",
                MainPage = "cloud",
                Items = new[]
                        new Item
                            Id = "240012549",
                            Title = "AMD's Bold ARM Server Gambit",
                            Subtitle = "AMD's Bold ARM Server Gambit",
      Description = 
         @"By combining 64-bit ARM processors with server-side " + 
          "technology, the company that led the x86 " +
          "architecture into the 64-bit world is hoping to " +
          "reinvent the data center and give itself new life.",
                            Content = "Content for AMD's Bold ARM Server Gambit...",
                            MainPage = "cloud/amds-bold-arm-server-gambit/240012549"
                        new Item
                            Id = "120020304050",
                            Title = "How Atlassian moved a code base of 21,000 files with " +  
                                    "more than 47,000 commits on its best-selling product " +
                                    "from Subversion to Git without " +
                                    "stopping the development workflow.",
                            Subtitle = 
                               "Migrating from Subversion to Git and the Lessons Learned",
                            Description = "It is incredible how the cloud growths",
                            Content = 
                               "Content for Migrating from Subversion to Git and the " + 
                               "Lessons Learned...",
                            MainPage = "cloud/240009175"
            new Group
                Id = "2",
                Title = "Mobile",
                Subtitle = "Mobile Developer",
                Description = "Mobile feature articles",
                MainPage = "mobile"
            new Group
                Id = "3",
                Title = "Parallel",
                Subtitle = "Parallel Programming",
                Description = "Parallel feature articles",
                MainPage = "parallel"
            new Group
                Id = "4",
                Title = ".NET",
                Subtitle = "M-Dev",
                Description = "The world of Microsoft Developers",
                MainPage = "windows"
            new Group
                Id = "5",
                Title = "JVM Languages",
                Subtitle = "Powered by Java Virtual Machine",
                Description = "The world of JVM languages",
                MainPage = "jvm"
            new Group
                Id = "6",
                Title = "C/C++",
                Subtitle = "Powered by C/C++",
                Description = "The world of C/++ developers",
                MainPage = "cpp"

        /// <summary>
        /// Retrieves all the groups with their items
        /// GET api/values
        /// </summary>
        /// <returns>All the groups with their items</returns>
        public IEnumerable<Group> Get()
            return _groups.ToArray();

Notice that Group is DrDobbsApp.Entities.Group, thus, I'm using the class shared in the previously created PCL. It is really very easy to create a REST service with ASP.NET Web API, and I have a service ready to provide data to my Windows Store app.

Now, I just have to make sure it works correctly. First, it is necessary to check the base URL in the project Web configuration. To do this, right click on DrDobbsWebAPI in Solution Explorer, select Properties | Web and check the value for Project URL in case you're using your local IIS Web Server (see Figure 8).

Figure 8: Web properties for the ASP.NET Web API project with the Project URL details.

In my case, the Project Url is http://localhost:46037/, so I can retrieve the groups if I execute the solution and I enter the following URL in any Web browser: http://localhost:46037/api/groups. I just need to add api/groups to the base URL, as explained in the table. Figure 9 shows the partial results of http://localhost:46037/api/groups that executes the GroupsController.Get method that returns an IEnumerable<Group>.

Figure 9: Using a Web browser to test that the GET api/groups REST call is working OK.

Making Calls to a REST Service with System.Net.Http.HttpClient

Now that I've created the REST service, I'll consume it in the sample Grid app by using the new asynchronous methods provided by the new System.Net.Http.HttpClient. Thus, it is necessary to go back to the DrDobbsApp project and follow these steps:

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.