我需要知道是否可以设置自定义operationid或命名约定,我的意思是我知道操作过滤器可以覆盖如何生成operationId的方式
using Swashbuckle.Swagger;
using System.Web.Http.Description;
namespace Something
{
public class MultipleOperationsWithSameVerbFilter : IOperationFilter
{
public void Apply(
Operation operation,
SchemaRegistry schemaRegistry,
ApiDescription apiDescription)
{
if (operation.parameters != null)
{
operation.operationId += "By";
foreach (var parm in operation.parameters)
{
operation.operationId += string.Format("{0}",parm.name);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在SwaggerConfig.cs中
c.OperationFilter<MultipleOperationsWithSameVerbFilter>();
Run Code Online (Sandbox Code Playgroud)
现在这有助于改变招摇的描述,请检查以下内容:
一切都很好,现在我最终在一个更黑暗的地方,例如类似于可能的情况:在同一个控制器上我有两个端点
这个例子不太正确(最后一篇文章应该是get)但仍然假设webapi无法更改(新的控制器用于分离)对于这个特殊情况我会试着找出一种方法来为每个动作以某种方式生成operationId diffrent,但我的问题是:
是否有可能以某种方式装饰与[JsonIgnore]或[Route("customer/delete")]类似的控制器动作,以明确有关operationId的信息.
c# asp.net-web-api swagger asp.net-web-api-routing swashbuckle
当为现有的 500 多个控制器和相应的方法创建 Angular API 服务代理时,我们试图覆盖 Swashbuckle/Swagger IO CodeGen 命名约定。
目前正在将 Net Core 3 API 与 Angular Typescript 链接起来。
以下答案有效:
[HttpGet("{id:int}", Name = nameof(GetProductById))]
public IActionResult GetProductById(int id) // operationId = "GetProductById"'
[HttpGet("{id:int}", Name = "GetProductById")]
public IActionResult GetProductById(int id) // operationId = "GetProductById"'
Run Code Online (Sandbox Code Playgroud)
有没有办法在启动时循环遍历所有控制器和方法?名称应与控制器中操作方法的名称相同。
这也许是可能的解决方案,但是,我需要行动价值。
return services.AddSwaggerGen(c =>
{
c.CustomOperationIds(e => $"{e.ActionDescriptor.RouteValues["controller"]}_{e.HttpMethod}");
Run Code Online (Sandbox Code Playgroud)
我正在使用aspnet core 2.2创建Restful API。我有一个标准设置,其中每个模型都有一个带有GET和POST动作的自己的控制器。我使用的Swashbuckle.AspNetCoreNuGet包,并使用该文章从微软的文档。
现在,当我查看生成的swagger文件时,它具有多个GET和POST operationIds。如何在不使用的情况下配置自定义operationIds Swashbuckle.AspNetCore.Annotations?
这是我的Action方法的样子:
[HttpPost]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
public async Task<ActionResult<Response>> PostAsync([FromBody]Request request)
{
Response result = await _context.PostAsync(request);
return Ok(result);
}
Run Code Online (Sandbox Code Playgroud)
我有多个控制器,它们都遵循相同的模式。
我的启动类如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
...
}
Run Code Online (Sandbox Code Playgroud)
我已经看过 …