使用 ASP.NET Core Web API 重命名 Swashbuckle 6 (Swagger) 中的模型 [英] Rename model in Swashbuckle 6 (Swagger) with ASP.NET Core Web API

查看:26
本文介绍了使用 ASP.NET Core Web API 重命名 Swashbuckle 6 (Swagger) 中的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 ASP.NET Core Web API 的 Swashbuckle 6 (Swagger).我的模型以 DTO 作为后缀,例如,

I'm using Swashbuckle 6 (Swagger) with ASP.NET Core Web API. My models have DTO as a suffix, e.g.,

public class TestDTO {
    public int Code { get; set; }
    public string Message { get; set; }
}

如何在生成的文档中将其重命名为Test"?我尝试添加一个带有名称的 DataContract 属性,但这没有帮助.

How do I rename it to just "Test" in the generated documentation? I've tried adding a DataContract attribute with a name, but that didn't help.

[HttpGet]
public IActionResult Get() {
  //... create List<TestDTO>
  return Ok(list);
}

推荐答案

想通了...类似于这里的答案:Swashbuckle 重命名模型中的数据类型

Figured it out... similar to the answer here: Swashbuckle rename Data Type in Model

唯一的区别是该属性现在称为 CustomSchemaIds 而不是 SchemaId:

The only difference was the property is now called CustomSchemaIds instead of SchemaId:

options.CustomSchemaIds(schemaIdStrategy);

我没有查看 DataContract 属性,而是将其删除DTO":

Instead of looking at the DataContract attribute, I just have it remove "DTO":

private static string schemaIdStrategy(Type currentClass) {
    string returnedValue = currentClass.Name;
    if (returnedValue.EndsWith("DTO"))
        returnedValue = returnedValue.Replace("DTO", string.Empty);
    return returnedValue;
}

这篇关于使用 ASP.NET Core Web API 重命名 Swashbuckle 6 (Swagger) 中的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆