swashbuckle 相关问题

Swashbuckle是一个开源框架,它将Swagger和Swagger-ui添加到ASP.NET Web API项目中。

Swashbuckle.AspNetCore:“规范中未定义任何操作!”将“Microsoft.AspNetCore.Mvc.ApiExplorer”包更新到 2.2.0 后出现问题

我们有使用 Swagger 的 .net core 2.1 mvc webapi 项目。 我们使用以下包: 我们有使用 Swagger 的 .net core 2.1 mvc webapi 项目。 我们使用以下包: <PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" /> <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="3.0.0" /> <PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="4.3.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.1.2" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="2.2.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" /> swashbuckle 配置如下: services.AddMvcCore().AddVersionedApiExplorer(); services.AddApiVersioning(); services.AddSwaggerGen(); 在这样的设置中一切正常(/swagger/v1/swagger.json 具有所有操作和定义并且 UI 正确呈现 -> 具有所有控制器和操作等) 我们正在尝试将我们的 .net 核心项目从 2.1 迁移到 2.2 .net 核心。 为了做到这一点(没有警告)我们需要升级 Microsoft.AspNetCore.Mvc.ApiExplorer nuget 从 2.1.2 到 2.2.0. 在此 nuget 更新之后 swagger.json (/swagger/v1/swagger.json) 不包含任何 "paths": {} 和 "definitions": {} 这导致 swagger UI 不显示控制器/操作(它呈现:No operations defined in spec! 升级包后这些包版本更新: <PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.2.0" /> //was 2.1.2 <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" /> //was 2.1.1 我已经尝试将 swashbuckle 版本升级到 v4.0.0 但它并没有解决问题。 我还缺少哪些包需要更新,以便在使用 swagger.json 包时正确生成 Microsoft.AspNetCore.Mvc.ApiExplorer 2.2.0? 或者我错过了其他东西? 刚遇到同样的问题,我想我也会分享我的解决方案。版本 6.0.4. 我必须将 [ApiController] 属性添加到我的控制器。 Swagger 必须使用它来发现。 将控制器中的公共方法更改为 [NoAction] 属性。 此外,将所有具有显式操作方法的操作更改为 [HttpGet("api/get-customer")]、[HttpPost("api/save-customer")] 而不是 [Route("api/get-customer") ]. 我找到了解决这个问题的方法(也发布到:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1166#issuecomment-497800414) 简而言之:我需要更改我的 HOST 项目 SDK:从“Microsoft.NET.Sdk”到“Microsoft.NET.Sdk.Web”。 我没有明确地发现它应该是这样的(但默认的 Microsoft 模板在 .net core 2.2 中使用这个 SDK) 所以如果有人能解释为什么从 .net core 2.1 迁移到 2.2 后需要使用 .Web SDK 就太好了 验证装饰器 [ApiController] [Route("[controller]")] 同时调用 app.UseSwagger(); 和 app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Sonic App"); }); 也会导致此错误。 只需将 [ApiController] 属性添加到控制器。它适用于 Swashbuckle.AspNetCore 6.4.0 我有同样的问题,问题是控制器中的访问修饰符是内部的而不是公共的。更换一切后再次工作。

回答 7 投票 0

Swashbuckle 不会为 Enum 生成架构

我有以下结构 //控制器 GetProducts(字符串 id,[FromQuery] 产品) //Product.cs 类包含一个枚举成员 产品 { 枚举示例A } `//startup.cs 服务.AddSwagger ...

回答 1 投票 0

如何将两个控制器拆分为两个 Swagger 定义

所以,我在 ASP.NET Web API 项目中制作了一个相当大的 API。我想有多个定义以确保分类器被正确分割,并在定义中使用标签。 在我的

回答 1 投票 0

NSwag 如何自定义 swagger .json 规范文件的 url

除了 /swagger/v1/swagger.json 之外,是否有任何方法可以指定生成的客户端代码从何处获取 swagger.json 我不想使用 NSwag 生成器,只使用 UI 我已经有了规格...

回答 0 投票 0

如何在编译时为我的 ASP.NET 项目中的每个控制器生成一个单独的 OpenAPI Swagger.json 文件?

我们的 ASP.NET Core Web 应用程序有一个不断增长的控制器列表。在项目的构建时,我想以编程方式为每个控件生成一个单独的 swagger.json 文件...

回答 0 投票 0

如何使用 Swashbuckle 提供模型文档和示例值?

我使用 Web API 2 (MVC 5) 创建了一个 API 方法,如下所示: /// /// 导入给定组织的所有工作。该方法假定组织的所有... 我已经使用 Web API 2 (MVC 5) 创建了一个 API 方法,如下所示: /// <summary> /// Import all of the jobs for the given organisation. This method assumes that all of the organisation's active jobs are present in the jobs array. /// To delete a job, simply exclude it from the jobs array. To delete all of the jobs, pass an empty array /// </summary> /// <param name="org">Organisation Id, provided by Shopless team</param> /// <param name="jobs">Full list of jobs which should be imported, json array</param> /// <response code="200">Jobs list have been queued for import (includes validation errors if any)</response> /// <response code="401">Access to this organisation was denied</response> /// <response code="404">Invalid organisation id</response> [SwaggerResponse(HttpStatusCode.BadRequest)] [SwaggerResponse(HttpStatusCode.NotFound)] [SwaggerResponse(HttpStatusCode.Unauthorized)] [HttpPost] [Route("org/{org}/jobs/full-import")] public IHttpActionResult FullImport(long org, [FromBody] List<JobApiDto> jobs) { if (!ModelState.IsValid) { return BadRequest("Jobs array is invalid"); } else if (org < 1) { return NotFound("Invalid organisation id"); } else if (!((ClaimsPrincipal)User).HasReadWriteAccessToOrganisation(org)) { return Unauthorized("Access to this organisation was denied"); } _apiProductUploader.Upload(org, jobs, out string message); return Accepted(message); } 上述方法接受JobApiDto的列表: public class JobApiDto { /// <summary> /// Job Id (unique id assigned to the job by the API consumer) /// </summary> /// <example>e5f52dae-e008-49bd-898b-47b5f1a52f40</example> public string UniqueThirdPartyId { get; set; } /// <summary> /// Short description which will be displayed in search result /// </summary> /// <example>Competitive salary, great team, cutting edge technology!</example> public string Synopsis { get; set; } // more properties 这就是 Swagger 文档的样子: 当我展开OrganisationJobs动作时: 这是模型选项卡: 如您所见,控制器的 xml 文档为 API 方法生成了正确的描述,但我无法理解为什么我为模型提供的描述(即 JobApiDto)没有显示? 此外,当我单击“示例值”时,什么也没有发生。 感谢亚历山大指出这个答案。 我安装了 Swashbuckle.Examples Nuget 包。 (文档很棒。) 我不得不用以下方式注释控制器: [SwaggerRequestExample(typeof(JobApiDto), typeof(ListJobApiDtoExample), jsonConverter: typeof(StringEnumConverter))] 这就是控制器定义的样子: /// <summary> /// Import all of the jobs for the given organisation. This method assumes that all of the organisation's active jobs are present in the jobs array. /// To delete a job, simply exclude it from the jobs array. To delete all of the jobs, pass an empty array /// </summary> /// <param name="org">Organisation Id, provided by Shopless team</param> /// <param name="jobs">Full list of jobs which should be imported, json array</param> [SwaggerRequestExample(typeof(JobApiDto), typeof(ListJobApiDtoExample), jsonConverter: typeof(StringEnumConverter))] [SwaggerResponse(HttpStatusCode.OK, Description = "Jobs array has been queued for import", Type = typeof(ImportResponse))] [SwaggerResponseExample(HttpStatusCode.OK, typeof(ImportResponseExamples))] [SwaggerResponse(HttpStatusCode.BadRequest, "Jobs array is invalid")] [SwaggerResponse(HttpStatusCode.NotFound, "Invalid organisation id")] [SwaggerResponse(HttpStatusCode.Unauthorized, "Access to this organisation was denied")] [HttpPost] [Route("org/{org}/jobs/full-import")] public IHttpActionResult FullImport(long org, [FromBody] List<JobApiDto> jobs) { if (!ModelState.IsValid) { return BadRequest("Jobs array is invalid"); } else if (org < 1) { return NotFound("Invalid organisation id"); } else if (!((ClaimsPrincipal)User).HasReadWriteAccessToOrganisation(org)) { return Unauthorized("Access to this organisation was denied"); } _apiProductUploader.Upload(org, jobs, out ImportResponse importResponse); return Accepted(importResponse); } 这里是ListJobApiDtoExample的实现: public class ListJobApiDtoExample : IExamplesProvider { public object GetExamples() { return new JobApiDto() { UniqueThirdPartyId = "e5f52dae-e008-49bd-898b-47b5f1a52f40", CategoryId = 1183, Title = "Senior Software Developer", Description = "Example company is looking for a senior software developer... more details about the job", Reference = "", Synopsis = "Competitive salary, great team, cutting edge technology!", MinSalary = 120000, MaxSalary = 140000, SalaryPer = "Per annum", DisplaySalary = true, CustomSalaryText = "plus bonus", WorkType = "Full time", Location = "Wellington CBD", ContactName = "John Smith", ContactEmail = "[email protected]", ContactPhoneNumber = "021 123 456 789", ApplicationUrl = "", LogoUrl = "https://www.example-company.com/my-company-logo", YouTubeVideo = "https://www.youtube.com/watch?v=khb7pSEUedc&t=1s", StartDate = null, PostedAt = null }; } } 请注意,我还有一个 API 返回类型的示例,ImportResponse。与之前的模型类似,我在控制器上有这个注释: [SwaggerResponseExample(HttpStatusCode.OK, typeof(ImportResponseExamples))] 这是植入: public class ImportResponseExamples : IExamplesProvider { public object GetExamples() { return new ImportResponse() { Message = "Products are queued to be imported" }; } } 现在,文档正确显示了示例:

回答 1 投票 0

swagger UI 在 webapi 中没有显示任何内容

我跟进到 xml 文档部分,以便使用 Swashbuckle 创建 Swagger 文档。它应该允许我通过(在我的例子中)查看端点: http://localhost:51854/swagger/ui/index

回答 6 投票 0

使用 Swashbuckle.AspNetCore.SwaggerUI 的默认端点/文档?

我在我的一个项目中使用 Swashbuckle.AspNetCore.SwaggerUI 包。我们正在使用 SwaggerEndpoint() 方法对端点进行版本控制,如下所示: app.UseSwaggerUI(选项=> { foreach(变量

回答 1 投票 0

是否可以扩展 Swagger/Swashbuckle 以使用新的 Http 方法?

我正在开发一个在 .net core 7 中使用 swashbuckle/swagger 的 api 项目。 我怎么遇到过一个问题,即由于查询长度有限,GET 请求不适合。和其他 Http

回答 0 投票 0

在 ASP.NET Core 3.0 Swagger 中将字符串标记为不可空

我使用的是 ASP.NET Core 3 和 Swashbuckle,大部分都是默认配置,我有一个 DTO 参数,上面有一个我希望不可为空的字符串。我怎样才能做到这一点?注意,必填和

回答 3 投票 0

Swagger用户界面显示camelCase参数,而不是PascalCase。

我在Asp.Net Core 3.1 API和Swashbuckle.AspNetCore 5.3.3版本中使用NewtonSoft.json。在Asp.Net Web API中,2个输入和输出参数的默认情况是PascalCase。现在我正在迁移到......。

回答 1 投票 0

全局授权过滤器不能与Swagger UI Asp.net Web Api一起使用。

我正在用我的Asp.net WEB Api项目实现Swagger UI,我使用了默认的System.Web.Http.AuthorizeAttribute,我已经在我的WebApiConfig.cs中的Register方法中把它注册为config......。

回答 1 投票 1

如何使用ASP.NET Core 2.2和Swashbuckle.AspNetCore 5.4.1针对.NET Framework 4.8生成Swagger文件。

创建一个新的ASP.NET Core 2.2项目,以.NET Framework 4.8为目标,按照 "入门 "添加Swashbuckle.AspNetCore,这个资源库:https:/github.comOrwel...

回答 1 投票 0

SwaggerSwashbuckle示例只限于一个pr请求对象的例子?

我一直在为使用Swashbuckle制作swagger请求示例而苦恼,因为我有几个端点使用相同的请求模型。我做了多个例子,将正确的例子应用到...

回答 1 投票 0

需要帮助我在SwashBuckle(AzureFunctions)上设置默认的API路由。

所以TL;DR我一直在学习如何将shwashbuckle纳入Azure功能,它一直在工作相当不错,到目前为止,但我不能改变我的默认路由前缀。我能够改变一些......

回答 1 投票 0

使用c.CustomOperationIds时检查OperationId的唯一性?

目前,如果你使用以下c.CustomOperationIds(apiDesc => { return apiDesc.TryGetMethodInfo(out MethodInfo methodInfo) ? methodInfo.Name : null; }); 而且,由于程序员的错误,...

回答 1 投票 0

Swagger json在.net core web api中显示了不需要的模式,如默认对象元数据。

我在asp.net Core web API项目中使用swagger(Swashbuckle.AspNetCore 5.4.1),它在组件中生成的JSON中有很多不需要的细节,如下图所示,但我希望它应该只是......

回答 1 投票 0


在Web API控制器操作中为swashbuckle / swagger添加默认值

[在C#Web Api控制器中,我如何修饰动作的参数,以使该参数的默认值显示为张扬?

回答 1 投票 1

在 IIS 上部署 API 时不显示 Swagger UI

好吧,我在我的 API 文档中使用 Swagger,它在本地主机上运行得很好,当我在 IIS 上托管它时,问题就开始了。出于某种原因,它不再起作用了 本地主机: https://

回答 6 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.