2013-03-28 19 views
0

我正在使用Web API检索SAS以将图片上传到Windows Azure blob存储。不幸的是它不起作用。有趣的是它可以在任何其他应用程序中工作,即使在Windows应用商店应用程序中也是如此这是我的控制器代码:Windows Phone 8和ASP.NET MVC Web API之间的通信

// 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项目创建样本值控制器工作无论是。

+0

我看到'api/users'和'api/values'..可能是2个控制器吗?..你能详细说明哪个/哪些部分无法正常工作吗? –

+0

从WP发出时,它们都不起作用。从任何其他平台进行调用时都可以工作。 –

+0

你的意思是你无法击中控制器?你是否看到404错误?...就像我不清楚什么不在这里工作... –

回答

1

你打电话给地址http://127.0.0.1:81/api/127.0.0.1指向您的Windows Phone设备,而不是您的本地服务器。改用你的机器的IP地址。

+0

我正在使用windows azure本地模拟器。这是Web API网站托管的地址。我应该如何改变这个地址? –

+0

好的,我已经将它改为http:// localhost:2330 /,但它仍然不起作用。 –

+0

@RadeMilovic不是本地主机,您的本地网络上的计算机的IP地址。你可以通过在控制台中输入命令'ipconfig'来获得它 –