簡體   English   中英

無法使用Web API在Azure ARM上創建存儲帳戶

[英]Unable to create Storage account on Azure ARM using Web API

我正在嘗試使用Web API創建一個存儲帳戶,但這給了我一個例外,該方法不允許,如下所示

Web Api異常

下面是我的代碼:

    string stAccName = "TCStorageAccount" + Guid.NewGuid().ToString().Substring(0, 8);
    stAccName = stAccName.ToLower();

    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

        using (var stream = new MemoryStream())
        using (var writer = new StreamWriter(stream))
        {
            var payloadText = JsonConvert.SerializeObject(parameters);

writer.Write(payloadText);
            writer.Flush();
            stream.Flush();
            stream.Position = 0;

            using (var content = new StreamContent(stream))
            {
                content.Headers.Add("Content-Type", "application/json");
                content.Headers.Add("x-ms-version", "2016-12-01");
                Uri uri = new Uri(String.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Storage/storageAccounts/{2}?api-version=2016-12-01", "subid", "resource group name", stAccName));

var response = await client.PostAsync(uri, content);

                if (!response.IsSuccessStatusCode)
                {
                    throw new InvalidOperationException(response.ReasonPhrase);
                }
           }
       }
   }

我也嘗試使用此線程中討論的另一種方法無法在Azure C#上創建存儲帳戶,但仍然無法創建存儲帳戶。

任何幫助,將不勝感激。 謝謝

您收到此錯誤的原因是因為“ Create Storage Account是一個PUT請求,而不是一個POST請求。

請更改以下代碼行:

var response = await client.PostAsync(uri, content);

var response = await client.PutAsync(uri, content);

並且不應出現此Method Not Allowed (405)錯誤Method Not Allowed (405)錯誤。

錯誤已刪除,但現在獲取新的異常`{StatusCode:400,ReasonPhrase:'Bad Request',Version:1.1'

異常是由於為存儲帳戶提供了錯誤的參數引起的。 響應返回后,您可以使用以下代碼查看詳細錯誤消息。

string detailError = await response.Content.ReadAsStringAsync();

這是一個示例參數,可以使用我這一邊的代碼成功創建一個存儲帳戶。

{
  "kind": "Storage",
  "location": "South Central US",
  "sku": {
    "name": "Standard_LRS"
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM