3. Creating a Model (MyFirstAzureApp.sln)
Now create a simple model to represent
the data (which will be a small set of sales data), and then expose
that data as a return value using the default controller. The example
uses the default plumbing code as much as possible to keep things
straightforward.
1. Right-click the Models folder and select Add ⇒ Class. Name the class Sales and then click Add.
2. Add the following bolded code to the Sales class, which provides you with a small set of properties for your custom Sales object.
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MyFirstAzureWebAPI.Models { public class Sales { public string Company { get; set; } public string FY09Sales { get; set; } public string FY10Sales { get; set; } public string FY11Sales { get; set; } } }
Now that you’ve created a model (or class), you’ll use this class to create a return List collection object that your Web API service will return in JSON format.
3. Expand the Controllers folder and double-click the ValuesController class.
4. Amend the class with the following bolded code (note these are fictional companies and sales figures).
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using MyFirstAzureWebAPI.Models; namespace MyFirstAzureWebAPI.Controllers { public class ValuesController : ApiController { // GET api/values public List<Sales> Get() { List<Sales> mySales = new List<Sales>(); mySales.Add(new Sales { Company="Contoso", FY09Sales="$9,020,398,122,332.00", FY10Sales="$10,111,309,312,998.00", FY11Sales="$11,033,990,102,443.00" }); mySales.Add(new Sales { Company="Fabrikam", FY09Sales="$7,332,444,552,112.00", FY10Sales="$5,019,132,011,668.00", FY11Sales="$3,889,940,589,901.00" }); mySales.Add(new Sales { Company = "Wingtip", FY09Sales = "$9,032,522,000,129.00", FY10Sales = "$9,115,339,990,899.00", FY11Sales = "$9,001,439,321,666.00" }); return mySales; } ... } }
The code that you added uses the Sales object to create a List collection object. When you append the Web API URI with "api/values" it will return the three SalesList collection as a JSON formatted object.
objects within the
5. Press F6 to build.
6. When you’ve
added and successfully compiled the code, press F5 to debug the cloud
application. (The solution uses the local cloud emulator to debug the
Web API.)
7. When debugging, you’ll see the ASP.NET default page appear (within the 127.0.0.1 domain). When you append the "api/values" path to the URL (as in Figure 12), the Web API service passes back a JSON-formatted object.
How it Works
The MVC templates are a versatile set
of ASP.NET Web templates, and the Web API is a specific template that
enables you to easily build REST-based services for Windows Azure.
You’ll find that Cloud services (such as REST or WCF-based services)
will be important to your cloud development efforts for a number of
reasons, such as service reuse or application extensibility. In this
exercise, you built a REST API with the scaffolding provided by the MVC
templates, and the result was a REST service that returned a JSON
object.
The JSON object, shown in the following code (with purely fictional data), reflects the List collection you created in the Get method. In essence, this is a small set of data that is returned to you via the REST service call.
[ { "Company":"Contoso", "FY09Sales":"$9,020,398,122,332.00", "FY10Sales":"$10,111,309,312,998.00", "FY11Sales":"$11,033,990,102,443.00" }, { "Company":"Fabrikam", "FY09Sales":"$7,332,444,552,112.00", "FY10Sales":"$5,019,132,011,668.00", "FY11Sales":"$3,889,940,589,901.00" }, { "Company":"Wingtip", "FY09Sales":"$9,032,522,000,129.00", "FY10Sales":"$9,115,339,990,899.00", "FY11Sales":"$9,001,439,321,666.00" } ]
When deployed, the REST service would
behave similarly, except the endpoint would be configured to a
production URI (for example, http://myapp.cloudapp.net).
Congratulations! You’ve created your first
Windows Azure application; that is, a REST-based Web API that returns
JSON data. When you deploy it to a production environment, you can use
the REST service from a multitude of clients and process the JSON data
within your applications — whether it is SharePoint, Windows Phone,
Windows 8, or other device/tablet applications.
Even before you deploy your first application to
your Windows Azure account, you can use this REST service locally
within the cloud emulator environment with other applications. Although
you won’t deploy this application to Windows Azure now , you can right-click the cloud project
(MyFirstAzureApp) and select Publish. This invokes the Publish Windows
Azure Application dialog, which enables you to walk through a wizard
and deploy your application directly to your Windows Azure subscription
— which you can then manage within the Windows Azure portal. See Figure 13 for the Start page of the Publish Windows Azure Application wizard.
At this point, you should at least have a basic
understanding of what Windows Azure is, how to set up the Windows Azure
development environment, and the types of applications that you can
build using Windows Azure. You might now be asking yourself, “Why all
the Windows Azure hubbub?” When paired with SharePoint 2013, Windows
Azure becomes important in two ways:
- It is natively integrated within the SharePoint application
development and deployment experience — you use Windows Azure to build
and deploy cloud-hosted applications.
- You can also use Windows Azure in the broader cloud application
development experience — just like you could use an array of other Web
technologies and standards.
|