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

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

问题描述

我正在将Swashbuckle 6(Swagger)与ASP.NET Core Web API结合使用.我的模型以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);
}

推荐答案

弄清楚了...类似于此处的答案:

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天全站免登陆