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) 中的模型


基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30