0

我有一个 ASP.Net Core 2.0 Web API 项目,它利用 NSwag 来获得 Swagger 功能。问题是当我使用 SwaggerUI 测试 API 时,FromBody 参数不起作用。但是,当我使用 Postman 并为标题选择相同的正文和 application/json 时(如在 SwaggerUi 中),一切正常。
在调试器中,我注意到来自 Postman 的请求有一个斜杠,而来自 SwaggerUi 的请求没有。

这是控制器的操作:

[HttpPost("")]
[SwaggerResponse(HttpStatusCode.Created, typeof(BlobItem))]
public async Task<IActionResult> Create([FromBody] BlobItem item)
{
    if (item == null)
    {
        return this.BadRequest("the item is null");
    }

    var blobItem = await BlobHelpers.PostBlobItemAync(this.container, item);

    return this.CreatedAtRoute("GetBlobInfo", new { blobName = item.Name }, blobItem);
}

邮递员 -> 项目已填满
SwaggerUi -> 项目为空


我尝试在 Startup 的默认路由中添加一个斜杠

app.UseSwaggerUi(typeof(Startup).GetTypeInfo().Assembly, new SwaggerUiSettings()
{
    DefaultUrlTemplate = "{controller}/{action}/{id}/",
    DefaultPropertyNameHandling = NJsonSchema.PropertyNameHandling.CamelCase,
});

没有成功,也

services.AddRouting(options => options.AppendTrailingSlash = true);

在 Startup.ConfigureServices 中没有成功,这意味着请求仍然没有通过 SwaggerUi 的尾部斜杠。


如何强制使用斜杠,或者我可以做其他事情来确保 FromBody 参数正常工作?

4

0 回答 0