SiliverLight 调用MVC Web api方法



1、创建ASP.NET MVC4 Web应用程序,选择WebAPI模板

data-cke-saved-src=http://www.it165.net/uploadfile/files/2014/0211/2014021118475227.png

data-cke-saved-src=http://www.it165.net/uploadfile/files/2014/0211/2014021118475228.png

2、添加silverlight项目

data-cke-saved-src=http://www.it165.net/uploadfile/files/2014/0211/2014021118475231.png

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、运行如下:

运行前:

data-cke-saved-src=http://www.it165.net/uploadfile/files/2014/0211/2014021118475233.png

运行后:

data-cke-saved-src=http://www.it165.net/uploadfile/files/2014/0211/2014021118475234.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值