gpt4 book ai didi

c# - Swagger Swashbuckle 抽象类文档

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:53 31 4
gpt4 key购买 nike

我有具有以下属性的 Web API 请求模型:

public List<Feature> Features { get; set; }

功能是一个抽象类。我将从中派生出许多类:

public abstract class Feature
{
public string Title { get; set; }
}

public class ImageFeature : Feature
{
public string ImageUrl { get; set; }
}

显然,Swashbuckle 仅识别 Feature 属性并相应地生成文档。我如何显式声明 Feature 类的可能实现,以便 Swashbuckle 生成正确的文档?有没有我可以使用的属性,比如:

[SwaggerResponseType(typeof(ImageFeature))]
[SwaggerResponseType(typeof(AnotherFeature))]
public abstract class Feature

最佳答案

看看这个: http://swashbuckletest.azurewebsites.net/swagger/ui/index#!/InheritanceTest/InheritanceTest_Get

enter image description here

下面是该 Controller 背后的代码: https://github.com/heldersepu/SwashbuckleTest/blob/911bf68e0cf6af3ee5d8278e6dd988eda8c4dc8d/Swagger_Test/Controllers/InheritanceTestController.cs

using System.Web.Http;

namespace Swagger_Test.Controllers
{
public abstract class Feature
{
/// <summary>We all need a Title</summary>
public string Title { get; set; }
}

public class ImageFeature : Feature
{
/// <summary>Here goes your image URL</summary>
public string ImageUrl { get; set; }
}

public class InheritanceTestController : ApiController
{
public ImageFeature Get([FromUri]ImageFeature imageFeature)
{
return imageFeature;
}

public ImageFeature Post(ImageFeature imageFeature)
{
return imageFeature;
}
}
}

关于c# - Swagger Swashbuckle 抽象类文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44705584/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com