问题描述
使用 Web API 并使用 swashbuckle 生成 swagger 文档,我在两个不同的命名空间中定义了两个具有相同名称的不同类.当我在浏览器中打开 swagger 页面时,它说
Using Web API and using swashbuckle to generate swagger documentation,I defined two different classes with the same name in two different namespaces. when I open swagger page in my browser it says
冲突的 schemaIds:检测到类型 A 和 B 的重复 schemaIds.请参阅配置设置 - UseFullTypeNameInSchemaIds"以获取潜在的解决方法
完整信息:
500 : {"Message":"发生错误.","ExceptionMessage":"冲突的 schemaIds:检测到类型 A 和 B 的重复 schemaIds.请参阅配置设置 - "UseFullTypeNameInSchemaIds" 以了解潜在的解决方法","ExceptionType":"System.InvalidOperationException","StackTrace":" at Swashbuckle.Swagger.SchemaRegistry.CreateRefSchema(Type type) at Swashbuckle.Swagger.SchemaRegistry.CreateInlineSchema(Type type) at Swashbuckle.Swagger.SchemaRegistry.b__1f(JsonProperty prop) 在 System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func
2 keySelector, Func2elementSelector, IEqualityComparer
1 比较器) 在 Swashbuckle.Swagger.SchemaRegistry.CreateObjectSchema(JsonObjectContract jsonContract) 在 Swashbuckle.Swagger.SchemaRegistry.CreateDefinitionSchema(Type type) 在 Swashbuckle.Swagger.SchemaRegistry.GetOrRegister(Type type) at Swashbuckle.Swagger.SwaggerGenerator.CreateOperation(ApiDescription apiDesc, SchemaRegistry schemaRegistry) 在 Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable1 apiDescriptions, SchemaRegistry schemaRegistry) 在 Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.<GetSwagger>b
2 group) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func
2 keySelector, Func2 elementSelector, IEqualityComparer
1 个比较器) 在 Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl, String apiVersion) 在 Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancelationToken)System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellingToken) 在 System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancelationToken) 在 System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage 请求,取消llationToken 取消令牌) 在 System.Web.Http.HttpServer.d__0.MoveNext()"} http://本地主机:24215/swagger/docs/v1
我不想更改班级名称.我该如何解决?
I don't want to change my classes' names. How can I fix it?
推荐答案
我终于在 swagger 配置中找到了方法.转到 App_StartSwaggerConfig.cs
文件并在 EnableSwagger
lambda 表达式下添加以下行:
I finally found a way in swagger configs. Go to App_StartSwaggerConfig.cs
file and under EnableSwagger
lambda expression add this line:
c.SchemaId(x => x.FullName);
完整代码是这样的:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
// your configs...
c.SchemaId(x => x.FullName);
// other configs...
})
.EnableSwaggerUi(c =>
// ....
});
这篇关于招摇错误:冲突的schemaIds:检测到类型A和B的重复schemaIds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!