Customising swagger ui to separate documentation

时间:2016-04-04 16:46:42

标签: asp.net-web-api2 swagger swagger-ui swashbuckle

I have integrated swagger into my Web API/OData project using swashbuckle 5.x and swashbuckle-Odata. If I navigate to http://root_url/swagger I can see all available API documentation in one big list. 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. I know this is something which I need to do by create a custom index.html for swagger and inject into swaggerconfig.cs like

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. 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.

+ 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

2 个答案:

答案 0 :(得分:1)

我可能误解了您的要求,但您应该能够使用SwaggerConfig.cs中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);
}

答案 1 :(得分:0)

You might want to use @Api annotation with tags attribute provided by Swagger. It will organize your APIs on swagger UI dashboard the way you want. E.g.

@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. enter image description here