public class ValuesController : ApiController
{
public IHttpActionResult Get()
{
return Ok (new []{"a","b"});
}
}
Result from the Google Chrome Web Browser:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>a</string>
<string>b</string>
</ArrayOfstring>
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int Volume { get; set; }
[ForeignKey("Company")]
public int CompanyID { get; set; }
public Company Company { get; set; }
- Notice that the Attributes [Key] is used to specify that Id is the primary key of your table, on the other hand [ForeignKey("Company")] is stating that CompanyID is the Foreign Key of the table's column Company.
public class Company
{
[Key]
public int Id { get; set; }
public string CompanyName { get; set; }
}
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<ArrayOfCoffee xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebAPIDemo.Models">
<Coffee>
<Company i:nil="true"/>
<CompanyID>1</CompanyID>
<Id>1</Id>
<Name>Long Black</Name>
<Volume>100</Volume>
</Coffee>
<Coffee>
<Company i:nil="true"/>
<CompanyID>1</CompanyID>
<Id>2</Id>
<Name>Capucino</Name>
<Volume>200</Volume>
</Coffee>
</ArrayOfCoffee>
- Notice that the company column is currently returning null, to return a value from that column:
public IQueryable<Coffee> GetCoffees()
{
return db.Coffees.Include(a=> a.Company);
}
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<ArrayOfCoffee xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebAPIDemo.Models">
<Coffee>
<Company>
<CompanyName>Starkbucks</CompanyName>
<Id>1</Id>
</Company>
<CompanyID>1</CompanyID>
<Id>1</Id>
<Name>Long Black</Name>
<Volume>100</Volume>
</Coffee>
<Coffee>
<Company>
<CompanyName>Starkbucks</CompanyName>
<Id>1</Id>
</Company>
<CompanyID>1</CompanyID>
<Id>2</Id>
<Name>Capucino</Name>
<Volume>200</Volume>
</Coffee>
</ArrayOfCoffee>
Notice that the company object's properties CompanyName and Id 'values are now being displayed.
<TextBlock x:Name="CoffeeTextBlock" HorizontalAlignment="Left" Margin="129,117,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="132" Width="238"/>
public class Coffee
{
public int Id { get; set; }
public string Name { get; set; }
public int Volume { get; set; }
public int CompanyID { get; set; }
public Company Company { get; set; }
}
public class Company
{
public int Id { get; set; }
public string CompanyName { get; set; }
}
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Windows;
using WpfAppDemo.Models;
Notice that your localhost url might be different.
var client = new HttpClient();
var response = await client.GetAsync("http://localhost:5710/api/coffees");
var results= await response.Content.ReadAsAsync<IEnumerable<Coffee>>();
var sb = new StringBuilder();
foreach (var coffee in results)
{
var text = string.Format("Name: {0}, Volume: {1}, Company: {2}", coffee.Name, coffee.Volume, coffee.Company.CompanyName);
sb.AppendLine(text);
}
CoffeeTextBlock.Text = sb.ToString();
Notice that the WPF app retrieved, converted and displayed data from the web api app that we created previously.
@{
ViewBag.Title = "About";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>
@*Loading image*@
<div id='loadingmessage' >
<img src='http://digitalsynopsis.com/wp-content/uploads/2015/10/gif-icons-menu-transition-animations-eatstreet-loading.gif' />
</div>
@*Outputing data into the following div*@
<div id="coffeeData"></div>
@*Adding arbitrary javascript code*@
@section script
{
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$('#loadingmessage').show(); // show the loading message.
//We need to get the data and display it
$.ajax({
//From where we want to take information
url: "http://localhost:5710/api/coffees",
//This is the type of http command that we are making (get/post/delete/put)
type: "Get",
//Telling the system "if the data comes correctly then go to success method else go to error method"
success: function(data)
{
for(var i =0; i< data.length; i++)
//Appending (adding/displaying to div with id "coffeeData") the content to the database in a formated way.
$('#coffeeData').append("<p>Name: " + data[i].Name + " " + data[i].Volume + "</p>");
$('#loadingmessage').hide(); // hide the loading message
},
error: function (msg)
{
alert(msg);
}
});
</script>
}
Notice that your jQuery file reference might be different than the one provided in the above code.
@RenderSection("script")
Notice that the database content loads soon after the web page loaded; this is helpful when you only want to load a specific content instead of loading the whole page again.