简体   繁体   English

.net core/swagger - 生成 swagger.json 文件时如何绕过控制器文件?

[英].net core/swagger - How can I around a controller file when generating the swagger.json file?

I have been handled a project and trying to add a swagger document for it.我已经处理了一个项目并试图为它添加一个 swagger 文档。 (using Swashbuckle.AspNetCore) (使用 Swashbuckle.AspNetCore)

I should have configured everything right.我应该正确配置一切。

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new Info { Title = "HCP API", Version = "v1" });
    var xmlPath = Path.Combine(AppContext.BaseDirectory, "XXXXXXXX.WebForApi.xml");
    c.IncludeXmlComments(xmlPath);
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "HCP Api V1");
});

However, I've got an exception when generating the swagger.json file.但是,生成 swagger.json 文件时出现异常。

System.NotSupportedException: Ambiguous HTTP method for action - XXXXXXXXX.Frameworks.Users.ApiControllers.SystemUserController.ApiModel (XXXXXXXXX.Framework.Users). Actions require an explicit HttpMethod binding for Swagger 2.0
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable`1 apiDescriptions, ISchemaRegistry schemaRegistry)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItems(IEnumerable`1 apiDescriptions, ISchemaRegistry schemaRegistry)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath, String[] schemes)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()

The file caused this error is a DLL file which includes in the project via NuGet.导致此错误的文件是通过 NuGet 包含在项目中的 DLL 文件。 (It's from a vendor company, I'm not sure that I was allowed to show its name.) (它来自一家供应商公司,我不确定我是否被允许展示它的名字。)

I have tried to add Filter to avoid this controller file, but it does not run into the filter code.我试图添加过滤器来避免这个控制器文件,但它没有运行到过滤器代码中。 I'm not sure I add it correctly.我不确定我是否正确添加了它。

It's anyway to solve this problem?反正要解决这个问题?

Yes, you need to add a filter to control this behavior.是的,您需要添加过滤器来控制此行为。 Here is some code which help you to include the filter.这里有一些代码可以帮助您包含过滤器。

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
    c.DocumentFilter<HideInDocsFilter>();
});

Here my pseudo filter code.这是我的伪过滤器代码。

public class HideInDocsFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        foreach (var apiDescription in context.ApiDescriptions)
        {
            if(apiDescription.RelativePath.Equals("SystemUser", System.StringComparison.OrdinalIgnoreCase))
            {
                var route = "/" + apiDescription.RelativePath.TrimEnd('/');
                swaggerDoc.Paths.Remove(route);
            }
        }
    }
}

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

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