当我尝试从windows phone 8应用程序访问在localhost上运行的ASP.NET Web Api 2应用程序时,我遇到了问题。我已经尝试并寻找了很多例子如何做但没有结果。我用Fiddler测试了我的api并且它有效。我测试从Web应用程序访问它,它工作,但当我尝试从Windows Phone 8应用程序访问它时,它没有。你能告诉我如何配置我的Windows Phone 8模拟器来访问它吗?提前谢谢。
答案 0 :(得分:1)
模拟器是一个虚拟机,所以" localhost"在手机应用程序内部意味着模拟器本身,而不是您正在运行Web服务的主机。要访问它,您必须在本地网络中提供主机的实际IP地址,而不是" localhost"。您可以通过在cmd控制台中运行ipconfig来查看您的IP。
答案 1 :(得分:0)
这是一篇关于访问Windows Phone 8中的web api方法的详细文章
Calling Web API from a Windows Phone 8
这里具体说明如何从Web API获取数据
string apiUrl = @"http://www.contoso.com/api/Books";
WebClient webClient = new WebClient();
webClient.Headers["Accept"] = "application/json";
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadCatalogCompleted);
webClient.DownloadStringAsync(new Uri(apiUrl));
private void webClient_DownloadCatalogCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
this.Items.Clear();
if (e.Result != null)
{
var books = JsonConvert.DeserializeObject<BookDetails[]>(e.Result);
int id = 0;
foreach (BookDetails book in books)
{
this.Items.Add(new ItemViewModel()
{
ID = (id++).ToString(),
LineOne = book.Title,
LineTwo = book.Author,
LineThree = book.Description.Replace("\n", " ")
});
}
this.IsDataLoaded = true;
}
}
catch (Exception ex)
{
this.Items.Add(new ItemViewModel()
{
ID = "0",
LineOne = "An Error Occurred",
LineTwo = String.Format("The following exception occured: {0}", ex.Message),
LineThree = String.Format("Additional inner exception information: {0}", ex.InnerException.Message)
});
}
}
希望这有帮助