Rename model in Swashbuckle 6 (Swagger) with ASP.NET Core Web API(使用 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) 中的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 ASP.NET Core Web API 重命名 Swashbuckle 6 (Swagger) 中的模型


基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01