silverlight调用MVC WebApi方法
2020-12-13 04:36
标签:style blog http color os 数据 1、创建ASP.NET MVC4 Web应用程序,选择WebAPI模板 2、添加silverlight项目 3、新建一个数据模型类,代码如下: 4、新建一个控制器,代码如下: 5、通过silverlight访问WebApi 6、运行如下: 运行前: 运行后: silverlight调用MVC WebApi方法,搜素材,soscw.com silverlight调用MVC WebApi方法 标签:style blog http color os 数据 原文地址:http://www.cnblogs.com/sky-net/p/3841676.htmlusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace NetMVCAPI.Models
{
public class Contact
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using NetMVCAPI.Models;
namespace NetMVCAPI.Controllers
{
public class ContactController : ApiController
{
Contact[] contacts = new Contact[]
{
new Contact(){ Id=1, Name="mk", Gender="男"},
new Contact(){ Id=2, Name="ll", Gender="男"},
new Contact(){ Id=3, Name="hj", Gender="男"},
new Contact(){ Id=4, Name="zxm", Gender="女"},
new Contact(){ Id=5, Name="wmq", Gender="女"},
};
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var uriStr = new Uri(Application.Current.Host.Source, TextBoxUri.Text);
var wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringAsyncCompleted);
wc.DownloadStringAsync(uriStr);
}
void DownloadStringAsyncCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
TextBlock_Result.Text = e.Result;
}
catch (Exception ex)
{
TextBlock_Result.Text = ex.Message;
}
}
}
}