简体   繁体   English

.net核心asp.net Web API操作返回意外状态代码

[英].net core asp.net web api action returning unexpected status code

I have set up this action: 我已设置此操作:

[HttpPost]
[ProducesResponseType(typeof(Category), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreateAsync(CategoryViewModel category)
{
    if (category == null) return BadRequest();
    if (!ModelState.IsValid) return BadRequest(ModelState);
    if (!category.Fields.Any()) return BadRequest();

    var request = Factory.Create(category);

    _categoryService.Save(request);
    await _categoryService.SaveChangesAsync();

    return Ok(category);
}

The response type should be Category with a status code of 201, but when I run my tests I get a status code of 200. This is my test: 响应类型应该是类别 ,其状态码为201,但是当我运行测试时,我得到的状态码为200。这是我的测试:

    [Test]
    public async Task CreateCategory()
    {
        // Assemble
        const string categoryId = "cameras";
        var services = ControllerContext.GivenServices();
        var controller = services.WhenCreateController();
        var field = new Field { CategoryId = categoryId, Name = "Title" };
        var category = new CategoryViewModel { Id = categoryId, Name = "Cameras", Fields = new List<Field>{ field } };

        // Act
        var result = await controller.CreateAsync(category);
        var okObjectResult = result as OkObjectResult;

        // Assert
        okObjectResult.Should().NotBeNull();
        Assert.AreEqual(StatusCodes.Status201Created, okObjectResult?.StatusCode);

        services.MockCategoryService.Verify(m => m.Save(It.IsAny<Category>()), Times.Once);
        services.MockCategoryService.Verify(m => m.SaveChangesAsync(), Times.Once);
    }

This test fails on this line: Assert.AreEqual(StatusCodes.Status201Created, okObjectResult?.StatusCode); 该测试在此行失败: Assert.AreEqual(StatusCodes.Status201Created, okObjectResult?.StatusCode); because it is returning 200 instead. 因为它返回200。

Does anyone know why? 有人知道为什么吗?

The ProducesResponseType-Attribute is not what determines what your Action returns, it is used for things like swagger/OpenAPI to create documentation . ProducesResponseType-Attribute不是决定Action返回什么的属性, 它用于诸如swagger / OpenAPI之类的操作来创建文档

You need to change the return value of your Action to return Created(UriOfTheCreatedResource, category) instead of return Ok(category) . 您需要更改Action的返回值以return Created(UriOfTheCreatedResource, category)而不是return Ok(category)

Documentation 文献资料

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回自定义状态代码在ASP.Net Web API中不起作用 - Returning custom status code is not working in ASP.Net Web API ASP.NET Core 3.1 中 API Controller 的单元测试返回错误的状态代码 - Unit test for API Controller in ASP.NET Core 3.1 returning a wrong status code 对ASP.NET WEB API的Ajax调用返回200状态代码错误 - Ajax call to ASP.NET WEB API returning error for 200 status code 使用HttpPut时,ASP.NET Web API始终返回405“不允许的方法”状态代码 - ASP.NET Web API keeps returning a 405 'Method Not Allowed' status code when using HttpPut ASP.Net Web API中的委托处理程序返回错误的状态代码 - Delegating Handler in ASP.Net Web API returning incorrect status code ASP.NET Core Web Api 是否不期望返回状态码 - Does ASP.NET Core Web Api not expect to return a status code asp.net 核心 3 Web Api ! 将 json 发送到 POST 操作 - asp.net core 3 Web Api ! send json to POST Action 状态码 406 使用 ASP.NET 内核中的格式响应数据 Web API - Status code 406 using Format response data in ASP.NET Core Web API Web 中的状态代码页 ASP.NET 核心中的 API 应用程序 - 是或否 - Status Code Pages in Web API apps in ASP.NET Core - Yes or No 如何在 ASP.NET CORE 3.1 Web API 中捕获当前状态码 - How to capture current Status code in ASP.NET CORE 3.1 Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM