【发布时间】:2018-02-15 03:14:12
【问题描述】:
我正在尝试使用 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);
}
}
}
}
我还尝试了此线程Unable to create Storage account on Azure C# 中讨论的另一种方法,但仍然无法创建存储帐户。
任何帮助将不胜感激。谢谢
【问题讨论】:
-
为什么不使用NET Client API?
-
感谢您的回复。因为我在使用 .net 客户端时遇到问题,如此链接 stackoverflow.com/questions/45984311/… 中所述
-
创建存储帐户是
PUT操作,而不是POST操作。请将您的方法从PostAsync更改为PutAsync应该可以解决这个问题。
标签: c# asp.net-mvc azure web