Asp.net-Web-Api
如何在 Swashbuckle 生成的帶有 OWIN 的 WebApi 2 的 Swagger 文件中包含類和屬性描述?
在你想到它之前,這是不一樣的。
我認為這應該是不言自明的。我想在 Swagger 文件中包含類描述。我的
Swagger配置如下所示:config.EnableSwagger(c => { c.SingleApiVersion("v1", "My Api Name"); c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>(); c.IncludeXmlComments(GetXmlCommentsPath()); }).EnableSwaggerUi(c => { });
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"); } }我的
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; } }如果不僱用 Skeet 先生,這可能嗎?
嗯……所以也許如果其他人遇到這個。
基本上我找到了一種可以做到這一點的方法,我意識到為什麼預設情況下沒有這樣做。不確定這是否是最好的方法,但它就在這裡。
在我的解決方案中,POCO 位於與實際 API 分開的項目中,因此
MyAwesomeModel沒有包含註釋描述,因為沒有為類和屬性生成 XML 節點。因此,在我的 POCO 所在的單獨項目中,我修改了屬性以生成 XML。
- 為 POCO 所在的項目生成 XML
- 確保將 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 => { });現在,在 Swagger 頁面上,如果我從 切換到
Model Schema,Model我現在可以閱讀整個模型和屬性描述。自然,不需要複製 XML 文件,可能只是在第 3 步中指向正確的位置,
GetXmlCommentsPathForModels());但這是我的選擇。


