Asp.net web api Windows Phone 8与ASP.NET MVC Web API之间的通信

Asp.net web api Windows Phone 8与ASP.NET MVC Web API之间的通信,asp.net-web-api,windows-phone-8,azure-storage-blobs,Asp.net Web Api,Windows Phone 8,Azure Storage Blobs,我正在使用Web API检索SAS,以便将图片上载到Windows Azure blob存储。不幸的是,它不起作用。有趣的是,它适用于任何其他应用程序,甚至在Windows应用商店应用程序中。这是我的控制器代码: // GET api/users?container="test"&blobname="test.jpg" public string Get(string container, string blobname) { try {

我正在使用Web API检索SAS,以便将图片上载到Windows Azure blob存储。不幸的是,它不起作用。有趣的是,它适用于任何其他应用程序,甚至在Windows应用商店应用程序中。这是我的控制器代码:

// GET api/users?container="test"&blobname="test.jpg"
    public string Get(string container, string blobname)
    {
        try
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("ConnectionString"));
            //CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); ;
            CloudBlobContainer blobContainer = blobClient.GetContainerReference(container);
            blobContainer.CreateIfNotExist();

            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
            containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;

            // Define a 4 hour window that the Windows 8 client can write to Azure Blob Storage.
            containerPermissions.SharedAccessPolicies.Add("mypolicy", new SharedAccessPolicy()
            {
                Permissions = SharedAccessPermissions.Write, // | SharedAccessPermissions.Read ,
                //To be available immediately don't set SharedAccessStartTime =
                SharedAccessExpiryTime = DateTime.Now.Add(TimeSpan.FromHours(4))
            });

            // Set the permissions so that Windows 8 client can write to the container
            // for the 4 hours specified above.
            blobContainer.SetPermissions(containerPermissions);

            // Create the shared access signature that will be added to the URL.
            string sas = blobContainer.GetSharedAccessSignature(new SharedAccessPolicy(), "mypolicy");

            // Creat the URI to be return to the Windows 8 client that will be used to write
            // to blob storage.
            return string.Format("{0}/{1}{2}", blobContainer.Uri, blobname, sas);

        }
        catch
        {
            throw new HttpResponseException(HttpStatusCode.InternalServerError);
        }
    }
这是客户:

string photoSasUrl = "http://127.0.0.1:81/api/users?container={0}&blobname={1}";
                    using (HttpClient client = new HttpClient())
                    using (var response = await client.GetAsync(String.Format(photoSasUrl, "profiles-pictures", "test.jpg")))
                    {
                        // Retrieve Shared Access Signature from Web Service
                        var sasUrl = await response.Content.ReadAsStringAsync();
                        // Trim any miscellaneous quotes
                        sasUrl = sasUrl.Trim('"');

                        // Load bytes of image into content object
                        var content = new StreamContent(e.ChosenPhoto);
                        // Content-Type will be image/jpeg
                        content.Headers.Add("Content-Type", "image/jpg");
                        // Write the bytes of the photo to blob storage
                        using (var uploadResponse = await client.PutAsync(new Uri(sasUrl), content))
                        {
                            if (uploadResponse.IsSuccessStatusCode)
                            {
                                // If successful, show on screen
                                MessageBox.Show("Upload Successful");
                            }
                        }
                    }
                    WebClient wc = new WebClient();
                    wc.DownloadStringCompleted += wc_DownloadStringCompleted;
                    wc.DownloadStringAsync(new Uri("http://127.0.0.1:81/api/values"));
                }
                catch { }

我发现它对默认情况下在web API项目创建时生成的示例值控制器也不起作用。

您正在调用地址
http://127.0.0.1:81/api/
127.0.0.1
指向Windows Phone设备,而不是本地服务器。请改用您机器的IP地址。

我看到一个“api/users”和“api/values”…可能有两个控制器?。您能否详细说明哪些/哪些不工作?从WP调用时,这两个都不工作。当从任何其他平台调用时,两者都可以工作。您的意思是无法点击控制器?你看到404错误了吗?…好像我不清楚这里有什么不工作…是的,我每次在Windows Phone上都会收到404找不到响应消息。我正在使用Windows azure本地模拟器。它是web API站点的托管地址。如何更改该地址?@RadeMilovic Not localhost,本地网络上计算机的IP地址。您可以通过在控制台中键入命令
ipconfig
来获取它。获取此:PPP适配器BENETON:IPv4地址………:10.10.250.82子网掩码………:255.255.255.255以太网适配器vEthernet(内部以太网端口Windows Phone Emulator内部交换机):IPv4地址………:169.254.80.80子网掩码………:255.255.0.0默认网关………:以太网适配器vEthernet(Realtek PCIe GBE系列控制器虚拟交换机):IPv4地址………:10.250.250.80子网掩码………:255.255.255.0我认为这两个ip地址不相同。@RadeMilovic不确定哪个ip地址是正确的,但我会选择
169.254.80.80
。但基本上,问题在于Windows Phone应用程序是在模拟器中运行的。这与应用程序在单独的设备上运行几乎是一样的
localhost
127.0.0.1
(两者都是等效的)表示“应用程序运行的设备”。它无法与您的Windows Phone应用一起使用,因为它正在单独的虚拟设备上运行(而您尝试过的其他应用,如Windows应用商店应用,则直接在您的计算机上运行)。