Asp.Net Core API を開発しています。
私のコントローラー宣言
[ApiController]
public class BarController : Controller
{
...
}
私のエンドポイントは次のようになります
[HttpPost, Route("bars")]
public async Task<ActionResult> DoAsync(
[FromBody] UpdateBars command)
{
// Do something with the command
return Ok(result);
}
コマンドは次のようになります
public class UpdateBars
{
[Required]
public IEnumerable<string> Ids { get; set; }
// ... more properties
}
互換性レベルは 2.1 に設定されています
public IServiceProvider ConfigureSharedServices(IServiceCollection services)
{
// ...
services.AddMvc()
.AddControllersAsServices()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// ...
}
古い質問: Ids パラメータが欠落している 400 の不正なリクエストが返されることを期待していましたが、バインディング エラーが返されませんでした。私は何を間違っていますか?
更新された質問: Ids パラメータが欠落しているか空である 400 の不正なリクエストが返されることを期待しています。パラメーターが欠落している (null) 場合、応答は期待どおりですが、空のコレクションの場合は 200 ok を返します。パラメータが存在するが空のときに悪いリクエストを受け取るように何かを変更することは可能ですか?