ASP.NET网页API HttpPost任务返回的204,而不是201 [英] ASP.NET Web Api HttpPost Task returning a 204 instead of 201

查看:1047
本文介绍了ASP.NET网页API HttpPost任务返回的204,而不是201的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的ASP.NET Web API的信息和方法之一公开为POST操作。它工作正常,但如预期返回204,而不是201 HTTP结果。下面是该方法的定义:

I am using ASP.NET Web Api and one of the methods is exposed as a POST operation. It works fine, but it returns a HTTP Result of 204 instead of 201 as expected. Here is the method definition:

[HttpPost][ActionName("Save")]
public Task SaveGameState(Guid instanceId, [FromBody] ComparisonGameState state)
{
    return gameInstancesClient.SaveGameState(instanceId, state);
}

是,它返回一个任务混淆的Web API?

Is the fact that it is returning a Task confusing Web Api?

我可以做,而不是下面的,但似乎有点小题大做:

I could do the following instead, but it seems like overkill:

[HttpPost][ActionName("Save")]
public async Task<HttpResponseMessage> SaveGameState(Guid instanceId, [FromBody] ComparisonGameState state)
{
    await gameInstancesClient.SaveGameState(instanceId, state);
    return new HttpResponseMessage { StatusCode = System.Net.HttpStatusCode.Created };
}

感谢您!

推荐答案

除非进一步的投入,这似乎是唯一可行的解​​决方案:

Barring further input, this seems to be the only workable solution:

[HttpPost][ActionName("Save")]
public async Task<HttpResponseMessage> SaveGameState(Guid instanceId, [FromBody] ComparisonGameState state)
{
    await gameInstancesClient.SaveGameState(instanceId, state);
    return new HttpResponseMessage { StatusCode = System.Net.HttpStatusCode.Created };
}

这篇关于ASP.NET网页API HttpPost任务返回的204,而不是201的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆