简体   繁体   English

自定义 swagger ui 以分离文档

[英]Customising swagger ui to separate documentation

I have integrated swagger into my Web API/OData project using swashbuckle 5.x and swashbuckle-Odata.我已经使用 swashbuckle 5.x 和 swashbuckle-Odata 将 swagger 集成到我的 Web API/OData 项目中。 If I navigate to http://root_url/swagger I can see all available API documentation in one big list.如果我导航到http://root_url/swagger,我可以在一个大列表中看到所有可用的 API 文档。 Every thing works perfectly fine but I have given a list of Odata controllers and API(s) which I need to show in a separate list .一切都很好,但我已经给出了Odata 控制器和 API列表,我需要在单独的列表中显示它们 I know this is something which I need to do by create a custom index.html for swagger and inject into swaggerconfig.cs like我知道这是我需要通过为 swagger 创建自定义 index.html 并注入 swaggerconfig.cs 来做的事情

c.CustomAsset("index", thisAssembly, "SwaggerUI_Config.SwaggerExtensions.index.html");

I have been researching on internet on how I can separate swagger documentation so that I can create a different HTML list and acheive my result, so far no luck.我一直在互联网上研究如何分离 swagger 文档,以便我可以创建不同的 HTML 列表并实现我的结果,到目前为止还没有运气。 Has anyone done something similar?有没有人做过类似的事情? Can you please give me some suggestions or pointer where I need to begin?.你能给我一些建议或指示我需要从哪里开始吗?

I'm trying to achive following structure on my swagger documentation.我正在尝试在我的 swagger 文档中实现以下结构。

+ Custom API list
  +API Controller #1
   > GET API
   > POST API
   > PUT API
   > DELETE API
  +API Controller #2
   > GET API
   > POST API
   > PUT API
   > DELETE API

+ All available API(s)
  +API Controller #1
   > GET API
   > POST API
   > PUT API
   > DELETE API
  +API Controller #2
   > GET API
   > POST API
   > PUT API
   > DELETE API
  +API Controller #3
   > GET API
   > POST API
   > PUT API
   > DELETE API

I may have misconstrued your requirements but you should be able to group your actions using the GroupActionsBy method in your EnableSwagger call in SwaggerConfig.cs:我可能误解了您的要求,但您应该能够在 SwaggerConfig.cs 中的EnableSwagger调用中使用GroupActionsBy方法对您的EnableSwagger进行分组:

c.GroupActionsBy(apiDesc =>
{
    string controllerName = apiDesc.ActionDescriptor.ControllerDescriptor.ControllerName;
    string method = apiDesc.ActionDescriptor.SupportedHttpMethods.First().Method;

    return string.Format("{0} {1} API", controllerName, method);
}

You might want to use @Api annotation with tags attribute provided by Swagger.您可能希望将 @Api 注释与 Swagger 提供的标签属性一起使用。 It will organize your APIs on swagger UI dashboard the way you want.它将按照您想要的方式在 swagger UI 仪表板上组织您的 API。 Eg例如

@Path("apiController1")
@Api(value = "/apiController1", tags = "API CONTROLLER 1")
@Produces({
        MediaType.APPLICATION_JSON
})
public class APIController1 {

    @GET
    @Path("fooGet")
    public final String fooGet() {
        return "Hello, World";
    }
}

In some other controller.在其他一些控制器中。

@Path("apiController2")
@Api(value = "/apiController2", tags = {
        "API CONTROLLER 2", "DEFAULT"
})
@Produces({
        MediaType.APPLICATION_JSON
})
public class APIController2 {

    @GET
    @Path("fooGet2")
    public final String fooGet2() {
        return "Hello, World";
    }
}

and the Swagger UI Dashboard will look like this. Swagger UI 仪表板将如下所示。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM