1、创建ASP.NET MVC4 Web应用程序,选择WebAPI模板
2、添加silverlight项目
3、新建一个数据模型类,代码如下:
01.
using System;
02.
using System.Collections.Generic;
03.
using System.Linq;
04.
using System.Web;
05.
06.
namespace NetMVCAPI.Models
07.
{
08.
public
class
Contact
09.
{
10.
public
int
Id { get; set; }
11.
12.
public
string Name { get; set; }
13.
14.
15.
public
string Gender { get; set; }
16.
}
17.
}
4、新建一个控制器,代码如下:
01.
using System;
02.
using System.Collections.Generic;
03.
using System.Linq;
04.
using System.Net;
05.
using System.Net.Http;
06.
using System.Web.Http;
07.
using NetMVCAPI.Models;
08.
09.
namespace NetMVCAPI.Controllers
10.
{
11.
public
class
ContactController : ApiController
12.
{
13.
Contact[] contacts =
new
Contact[]
14.
{
15.
new
Contact(){ Id=
1
, Name=
'mk'
, Gender=
'男'
},
16.
new
Contact(){ Id=
2
, Name=
'll'
, Gender=
'男'
},
17.
new
Contact(){ Id=
3
, Name=
'hj'
, Gender=
'男'
},
18.
new
Contact(){ Id=
4
, Name=
'zxm'
, Gender=
'女'
},
19.
new
Contact(){ Id=
5
, Name=
'wmq'
, Gender=
'女'
},
20.
};
21.
22.
/// <summary>
23.
/// /api/Contact
24.
/// </summary>
25.
/// <returns></returns>
26.
public
IEnumerable<contact> GetListAll()
27.
{
28.
return
contacts;
29.
}
30.
31.
/// <summary>
32.
/// /api/Contact/id
33.
/// </summary>
34.
///<param name="'id'">
35.
/// <returns></returns>
36.
public
Contact GetContactById(
int
id)
37.
{
38.
Contact contact = contacts.FirstOrDefault<contact>(item => item.Id == id);
39.
if
(contact ==
null
)
40.
{
41.
throw
new
HttpResponseException(HttpStatusCode.NotFound);
42.
}
43.
return
contact;
44.
}
45.
46.
/// <summary>
47.
/// 根据性别查询
48.
/// /api/Contact?Gender=女
49.
/// </summary>
50.
///<param name="'gender'">
51.
/// <returns></returns>
52.
public
IEnumerable<contact> GetListByGender(string gender)
53.
{
54.
return
contacts.Where(item => item.Gender == gender);
55.
}
56.
57.
/// <summary>
58.
/// 根据姓名查询
59.
/// /api/Contact/Name=mk
60.
/// </summary>
61.
///<param name="'name'">
62.
/// <returns></returns>
63.
public
IEnumerable<contact> GetListByName(string name)
64.
{
65.
return
contacts.Where(item => item.Name == name);
66.
}
67.
}
68.
}</contact></contact></contact></contact>
5、通过silverlight访问WebApi
01.
using System;
02.
using System.Collections.Generic;
03.
using System.Linq;
04.
using System.Net;
05.
using System.Windows;
06.
using System.Windows.Controls;
07.
using System.Windows.Documents;
08.
using System.Windows.Input;
09.
using System.Windows.Media;
10.
using System.Windows.Media.Animation;
11.
using System.Windows.Shapes;
12.
13.
namespace SilverlightApplication1
14.
{
15.
public
partial
class
MainPage : UserControl
16.
{
17.
18.
public
MainPage()
19.
{
20.
InitializeComponent();
21.
}
22.
23.
private
void
Button_Click(object sender, RoutedEventArgs e)
24.
{
25.
var uriStr =
new
Uri(Application.Current.Host.Source, TextBoxUri.Text);
26.
var wc =
new
WebClient();
27.
wc.DownloadStringCompleted +=
new
DownloadStringCompletedEventHandler(DownloadStringAsyncCompleted);
28.
wc.DownloadStringAsync(uriStr);
29.
}
30.
void
DownloadStringAsyncCompleted(object sender, DownloadStringCompletedEventArgs e)
31.
{
32.
try
33.
{
34.
TextBlock_Result.Text = e.Result;
35.
}
36.
catch
(Exception ex)
37.
{
38.
TextBlock_Result.Text = ex.Message;
39.
}
40.
}
41.
}
42.
}
6、运行如下:
运行前:
运行后: