我試圖使用Web API創建存儲帳戶,但它給我不同的是方法不被允許如下圖所示無法使用Web API
下面是在Azure上ARM創建存儲帳戶我的代碼:
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);
}
}
}
}
我也有另一種方式試圖爲這個線程Unable to create Storage account on Azure C#討論,但仍無法創建一個存儲帳戶。
任何幫助,將不勝感激。謝謝
爲什麼不使用[NET Client API](https://docs.microsoft.com/zh-cn/dotnet/api/overview/azure/storage?view=azure-dotnet)? –
感謝您的回覆。因爲我在使用.net客戶端時遇到問題,如本鏈接所述https://stackoverflow.com/questions/45984311/unable-to-create-storage-account-on-azure-c-sharp –
創建存儲帳戶是一個'PUT'操作,而不是'POST'操作。請將您的方法從'PostAsync'更改爲'PutAsync',這應該可以解決這個問題。 –