Mar*_*cus 10 asp.net-web-api swagger owin swashbuckle
在你想到它之前,这是不一样的.
我认为这应该是非常自我解释的.我想在Swagger文档中包含类描述.我的Swagger配置如下所示:
config.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "My Api Name");
c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>();
c.IncludeXmlComments(GetXmlCommentsPath());
}).EnableSwaggerUi(c => { });
Run Code Online (Sandbox Code Playgroud)
而且MyAwesomeController看起来是这样的:
/// <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");
}
}
Run Code Online (Sandbox Code Playgroud)
我MyAwesomeModel看起来像这样:
/// <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; }
}
Run Code Online (Sandbox Code Playgroud)
这可能不雇用Sr.Skeet吗?
Mar*_*cus 23
嗯...所以也许如果其他人碰到这个.
基本上我找到了一种方法可以完成,我意识到为什么默认情况下没有这样做.不确定它是否是最好的方法,但在这里它.
在我的解决方案中,POCO位于与实际API分开的项目中,因此MyAwesomeModel未包含注释描述,因为没有为类和属性生成XML节点.因此,在我的POCO所在的单独项目中,我修改了属性以生成XML.
Swashbuckle想要查找的任何路径.我Post-build event command line在项目属性中使用过;copy "$(SolutionDir)MyAwesomeProjectWithPocos\bin\MyAwesomeProjectWithPocos.xml" "$(ProjectDir)\bin\MyAwesomeProjectWithPocos.xml"
SwaggerConfig以包含此XML即
config.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "My Api Name");
c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>();
c.IncludeXmlComments(GetXmlCommentsPath());
c.IncludeXmlComments(GetXmlCommentsPathForModels());
}).EnableSwaggerUi(c => { });
Run Code Online (Sandbox Code Playgroud)
现在,在Swagger页面上,如果我切换Model Schema到ModelI,现在可以阅读整个模型和属性描述.
当然,没有要求复制XML文件,可能只是指向步骤#3中的正确位置,GetXmlCommentsPathForModels());但这是我的选择.
| 归档时间: |
|
| 查看次数: |
5357 次 |
| 最近记录: |