开箱即用的配置在我的机器上完美运行,完全没有问题.
但是当我部署到我们的测试环境时 - 我收到以下消息
500:{"消息":"发生了错误."/api/swagger/docs/v1
我猜它与baseUrl或类似的东西有关,但我甚至不知道从哪里开始.
我的路线在项目中运行良好 - 我可以调用我的所有webapi端点并且它们正确响应.
任何帮助将非常感激
我正在将 ASP.NET Core API 项目从 v5 升级到 v6。
v5中的服务配置:
services.AddSwaggerGen();
Run Code Online (Sandbox Code Playgroud)
v6中的服务配置:
builder.Services.AddEndpointsApiExplorer(); // what is this?
builder.Services.AddSwaggerGen();
Run Code Online (Sandbox Code Playgroud)
什么是AddEndpointsApiExplorer?无论我是否添加,一切都会按预期进行。
我使用“ASP.NET API 版本控制”库。他们有关系吗?如果是这样,我必须同时使用两者,只使用库,还是现在不需要库?
c# swashbuckle asp.net-core aspnet-api-versioning asp.net-core-6.0
有没有办法将所有枚举显示为swagger中的字符串值而不是int值?
我希望能够提交POST操作并根据其字符串值放置枚举,而不必每次都查看枚举.
我试过,DescribeAllEnumsAsStrings但服务器接收字符串而不是枚举值,这不是我们正在寻找的.
有人解决了这个吗?
编辑:
public class Letter
{
[Required]
public string Content {get; set;}
[Required]
[EnumDataType(typeof(Priority))]
public Priority Priority {get; set;}
}
public class LettersController : ApiController
{
[HttpPost]
public IHttpActionResult SendLetter(Letter letter)
{
// Validation not passing when using DescribeEnumsAsStrings
if (!ModelState.IsValid)
return BadRequest("Not valid")
..
}
// In the documentation for this request I want to see the string values of the enum before submitting: Low, Medium, High. Instead of 0, 1, 2
[HttpGet]
public …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中加入了swagger UI.
当我尝试看到swagger UI时,我很好地获得了API的文档但是在一段时间后它在按钮上显示了一些错误图标.
错误消息如下所示:
[{"level":"error","message":"无法从文件中读取 http:// MYIP/swagger/docs/v1 "}]
我不确定是什么导致它.如果我刷新它工作并在几秒后显示错误.
我正在尝试在新的asp .net CORE/MVC 6项目中设置一个基本的swagger API文档,并从swagger UI接收500错误:
500 : http://localhost:4405/swagger/v1/swagger.json
我的启动类中包含以下代码:
using Swashbuckle.SwaggerGen;
using Swashbuckle.SwaggerGen.XmlComments;
using Swashbuckle.Application;
....
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSwaggerGen();
services.ConfigureSwaggerDocument(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "Blog Test Api",
Description = "A test API for this blogpost"
});
});
}
Run Code Online (Sandbox Code Playgroud)
然后在Configure下:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
....
app.UseSwaggerGen();
app.UseSwaggerUi();
....
}
Run Code Online (Sandbox Code Playgroud)
当我构建并运行项目时,当我转到swagger/UI/index.html时,UI会出现,但会显示上面的500错误.当我转到swagger/v1/swagger.json链接时,控制台会给出以下500错误:
Failed to load resource: the server responded with a status of 500 (Internal Server Error) …
使用Web API并使用swashbuckle生成swagger文档,我在两个不同的命名空间中定义了两个具有相同名称的不同类.当我在浏览器中打开swagger页面时说
冲突的schemaIds:为类型A和B检测到重复的schemaId.请参阅配置设置 - "UseFullTypeNameInSchemaIds"以获取潜在的解决方法
完整消息:
500:{"消息":"发生了错误.","ExceptionMessage":"冲突的schemaIds:为类型A和B检测到重复的schemaIds.请参阅配置设置 - \"UseFullTypeNameInSchemaIds \"以获取潜在的解决方法","ExceptionType ":"System.InvalidOperationException","StackTrace":"在Swashbuckle.Swagger.SchemaRegistry.CreateRefSchema(类型类型)\ r \n,在Swashbuckle.Swagger.Swagger.SchemaRegistry.CreateInlineSchema(类型类型)\ r \n在Swashbuckle.Swagger. SchemaRegistry.b__1f(JsonProperty prop)\ r \n在System.Linq.Enumerable.ToDictionary [TSource,TKey,TElement](IEnumerable
1 source, Func2 keySelector,Func2 elementSelector, IEqualityComparer1 comparer )\ r \n在Swashbuckle.Swagger.SchemaRegistry.CreateObjectSchema(JsonObjectContract jsonContract) )\ r \n at Swashbuckle.Swagger.SchemaRegistry.CreateDefinitionSchema(Type type)\ r \n at Swashbuckle.Swagger.SchemaRegistry.GetOrRegister(Type type)\ r \n at Swashbuckle.Swagger.SwaggerGenerator.CreateOperation(ApiDescription apiDesc,SchemaRegistry) schemaRegistry)\ r \n在Swashbuckle.Swagger.SwaggerGenerator.CreateP athItem(IEnumerable的1 apiDescriptions, SchemaRegistry schemaRegistry)\r\n at Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.<GetSwagger>b__4(IGrouping2组)\ r \n在System.Linq.Enumerable.ToDictionary [TSource,TKEY的,TElement](IEnumerable的1 source, Func2的KeySelector,Func键2 elementSelector, IEqualityComparer1比较器)\ r \n在Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(字符串使用rootUrl,字符串apiVersion)\ r \n在Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)\ r \n在System.Net.Http.MttageInvoker.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)\ …
我无法获得Swashbuckle.AspNetCore(1.0.0)包来生成任何输出.我读过swagger.json文件应该写成'〜/ swagger/docs/v1'.但是,我没有得到任何输出.
我从一个全新的ASP.NET Core API项目开始.我应该提到这是ASP.NET Core 2. API工作,我能够从值控制器中检索值就好了.
我的启动类具有完全如本文所述的配置(GitHub上的Swashbuckle.AspNetCore).
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method …Run Code Online (Sandbox Code Playgroud) 我在文档中读到“.NET 的两个主要 OpenAPI 实现是 Swashbuckle 和 NSwag”,但它们之间有什么区别?
我熟悉Microsoft堆栈.我正在使用OData来完成我的一些宁静服务.最近我遇到了Swagger的API文档,我试图了解它与OData的关系.它们似乎都是RESTful规范.哪一个被广泛使用?
我正在使用Swashbuckle为webapi2项目生成swagger文档\ UI.我们的模型与一些传统接口共享,因此我想在模型上忽略一些属性.我不能使用JsonIgnore属性,因为旧版接口也需要序列化为JSON,所以我不想全局忽略这些属性,只是在Swashbuckle配置中.
我在这里找到了一种记录方法:
https://github.com/domaindrivendev/Swashbuckle/issues/73
但这似乎与目前的Swashbuckle版本已经过时了.
为旧版Swashbuckle推荐的方法是使用IModelFilter实现,如下所示:
public class OmitIgnoredProperties : IModelFilter
{
public void Apply(DataType model, DataTypeRegistry dataTypeRegistry, Type type)
{
var ignoredProperties = … // use reflection to find any properties on
// type decorated with the ignore attributes
foreach (var prop in ignoredProperties)
model.Properties.Remove(prop.Name);
}
}
SwaggerSpecConfig.Customize(c => c.ModelFilter<OmitIgnoredProperties>());
Run Code Online (Sandbox Code Playgroud)
但我不确定如何配置Swashbuckle在当前版本中使用IModelFilter?我正在使用Swashbuckle 5.5.3.
swashbuckle ×10
c# ×6
swagger ×4
swagger-ui ×4
asp.net-core ×2
.net ×1
nswag ×1
odata ×1
openapi ×1