【发布时间】:2015-08-12 08:45:07
【问题描述】:
我有一个 web api(在 ASP.NET Web API 2 上用 c# 编写)。其中一种方法采用 HTTP POST 并将其添加到 Azure 队列。
我想知道如何从 Azure 队列中获取 http 状态码(或任何可用的),以便我可以从我的方法中返回适当的 http 状态码。
到目前为止的代码是这样的:
[Route("api/v1/somewhere/")]
[ResponseType(typeof(Thingy))]
public async Task<IHttpActionResult> Post(Thingy thingy)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ToString());
var queueClient = storageAccount.CreateCloudQueueClient();
var queue = queueClient.GetQueueReference("thingys");
var msg = new CloudQueueMessage(JsonConvert.SerializeObject(thingy));
await queue.AddMessageAsync(msg);
return Json(thingy);
}
如果await queue.AddMessageAsync(msg); 一切顺利,我想换掉return Json(thingy); 行并返回http 状态代码202,如果出现问题,则返回另一个适当的代码。
有什么想法吗?
【问题讨论】:
-
我在下面添加了一个答案,如果它解决了你的问题,请告诉我
标签: c# azure asp.net-web-api asp.net-web-api2 azure-queues