1

我正在使用Swashbuckle.AspNetCore并添加x-codeSamples以下代码:

public class XCodeSamplesFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        string source = @"PetStore.v1.Pet pet = new PetStore.v1.Pet();
                        pet.setApiKey(""your api key"");
                        pet.petType = PetStore.v1.Pet.TYPE_DOG;
                        pet.name = ""Rex"";
                        // set other fields
                        PetStoreResponse response = pet.create();
                        if (response.statusCode == HttpStatusCode.Created)
                        {
                            // Successfully created
                        }
                        else
                        {
                            // Something wrong -- check response for errors
                            Console.WriteLine(response.getRawResponse());
                        }";
        // need to check if extension already exists, otherwise swagger 
        // tries to re-add it and results in error  

        if (!swaggerDoc.Info.Extensions.ContainsKey("x-codeSamples"))
            swaggerDoc.Info.Extensions.Add("x-codeSamples", new OpenApiObject
            {
                {"lang", new OpenApiString("C#")},
                {"label", new OpenApiString("")},
                {"source", new OpenApiString(source)},                        
            });
    }

}

我可以在 JSON 文件中看到这一点:

在此处输入图像描述

但代码示例未显示在 UI 中。请帮我看看怎么回事?

4

0 回答 0