首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在带有OWIN的WebApi 2的Swagger文档中包含类和属性描述?

如何在带有OWIN的WebApi 2的Swagger文档中包含类和属性描述?
EN

Stack Overflow用户
提问于 2016-09-19 22:54:37
回答 2查看 9.2K关注 0票数 12

在你想到它之前,this是不一样的。

我认为这应该是不言而喻的。我想在Swagger文档中包含类描述。我的Swagger配置如下所示:

代码语言:javascript
运行
复制
config.EnableSwagger(c =>
{
    c.SingleApiVersion("v1", "My Api Name");
    c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>();
    c.IncludeXmlComments(GetXmlCommentsPath());

}).EnableSwaggerUi(c => { });

MyAwesomeController看起来像这样:

代码语言:javascript
运行
复制
/// <summary>
/// Controller description (is included by Swashbuckle)
/// </summary>
public class MyAwesomeController : ApiController
{
    /// <summary>
    /// Method description (is included by Swashbuckle)
    /// </summary>
    public IHttpActionResult Get()
    {
        return Ok("hello... from the other side");
    }

    public IHttpActionResult Post([FromBody]MyAwesomeModel model)
    {
        return Ok("hello... from the other side");
    }
}

我的MyAwesomeModel是这样的:

代码语言:javascript
运行
复制
/// <summary>
/// **I would like this to be included in the Swagger description of the parameter**
/// </summary>
public class MyAwesomeModel
{
    /// <summary>
    /// **I would like this to be included in the Swagger description of the parameter**
    /// </summary>
    public string MyProperty { get; set; }
}

有没有可能在不雇佣斯基特的情况下实现?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-20 16:20:06

嗯..。所以也许如果其他人也遇到这种情况。

基本上,我找到了一种可以做到这一点的方法,并且我意识到为什么它不是默认的。不确定这是否是最好的方法,但它开始了。

在我的解决方案中,POCOs位于一个独立于实际API的项目中,因此没有包括MyAwesomeModel的注释描述,因为没有为类和属性生成XML节点。因此,在POCOs所在的单独项目中,我修改了属性以生成XML。

  1. 为POCO所在的项目生成XML

  1. 确保将XML复制到您希望Swashbuckle查找它的任何路径。我在项目属性中使用了Post-build event command line

copy "$(SolutionDir)MyAwesomeProjectWithPocos\bin\MyAwesomeProjectWithPocos.xml" "$(ProjectDir)\bin\MyAwesomeProjectWithPocos.xml"

  1. SwaggerConfig修改为也包含此XML

也就是说。

代码语言:javascript
运行
复制
config.EnableSwagger(c =>
{
    c.SingleApiVersion("v1", "My Api Name");
    c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>();
    c.IncludeXmlComments(GetXmlCommentsPath());
    c.IncludeXmlComments(GetXmlCommentsPathForModels());

}).EnableSwaggerUi(c => { });

现在,在Swagger页面上,如果我从Model Schema切换到Model,我现在可以读取整个模型和属性描述。

当然,不需要复制XML文件,只需在步骤#3 GetXmlCommentsPathForModels());中指向正确的位置,但这是我的选择。

票数 32
EN

Stack Overflow用户

发布于 2016-09-20 04:43:58

如果您已经将下面的语句

代码语言:javascript
运行
复制
c.IncludeXmlComments(GetXmlCommentsPath());

您是否可以检查xml注释路径方法是否返回放置Project xml文档文件的XML文件的路径?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39576189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档