wer*_*aco 6 asp.net asp.net-web-api swagger swagger-ui swashbuckle
我正在整理一个需要匹配外部源 XML 格式的 Web API,并希望在 swagger 输出中重命名数据类型对象。
它在类的成员上运行良好,但我想知道是否也可以覆盖类名。
例子:
[DataContract(Name="OVERRIDECLASSNAME")]
public class TestItem
{
[DataMember(Name="OVERRIDETHIS")]
public string toOverride {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
在生成的输出中,我最终看到了模型:
TestItem {
OVERRIDETHIS (string, optional)
}
Run Code Online (Sandbox Code Playgroud)
我希望看到
OVERRIDECLASSNAME { OVERRIDETHIS(字符串,可选)}
这可能吗?
谢谢,
我有同样的问题,我想我现在解决了。
首先在 Swagger 配置中添加 SchemaId(从 5.2.2 版开始,请参阅https://github.com/domaindrivendev/Swashbuckle/issues/457):
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SchemaId(schemaIdStrategy);
[...]
}
Run Code Online (Sandbox Code Playgroud)
然后添加这个方法:
private static string schemaIdStrategy(Type currentClass)
{
string returnedValue = currentClass.Name;
foreach (var customAttributeData in currentClass.CustomAttributes)
{
if (customAttributeData.AttributeType.Name.ToLower() == "datacontractattribute")
{
foreach (var argument in customAttributeData.NamedArguments)
{
if (argument.MemberName.ToLower() == "name")
{
returnedValue = argument.TypedValue.Value.ToString();
}
}
}
}
return returnedValue;
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
6165 次 |
| 最近记录: |