Sil*_*mor 12 c# asp.net-web-api swagger asp.net-web-api-routing swashbuckle
我需要知道是否可以设置自定义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的信息.
ven*_*rik 24
你可以使用SwaggerOperationAttributeSwashbuckle提供的.
[SwaggerOperation("get")]
public IEnumerable<Contact> Get()
{
....
}
[SwaggerOperation("getById")]
public IEnumerable<Contact> Get(string id)
{
...
}
Run Code Online (Sandbox Code Playgroud)
您可以使用该属性为您的操作添加标签和方案.看看源代码
mwi*_*son 15
对于 swashbuckle 5.0,您可以使用该Name属性。您可以将其设置为任何,string但我nameof喜欢这样使用:nameof(ActualMethodName).
[HttpGet("{id:int}", Name = nameof(GetProductById))]
public IActionResult GetProductById(int id) // operationId = "GetProductById"'
Run Code Online (Sandbox Code Playgroud)
或者
[HttpGet("{id:int}", Name = "GetProductById")]
public IActionResult GetProductById(int id) // operationId = "GetProductById"'
Run Code Online (Sandbox Code Playgroud)
有列出了一些其他选择在这里