I'm trying to get the format identifier printed on the Swagger UI which is stated on their homepage. In the auto-generated json file it should look like in this example:
format property example

我希望通过我的视图模型类中的XML注释来实现这一点.这对examplesummary等标签很有效,但没有format这样的标签.我必须说明的是,我们在API中等待的字符串必须是某种格式.

类似的问题也被问到了here,但格式识别符还没有答案.

我怎样才能做到这一点呢? 使用的版本:.NET7和Swashbakle v6.4.0.

我的视图模型如下所示:

public class MeasurementViewModel {
    /// <example>20.01.2003</example>
    /// <format>dd.mm.yyyy</format>  <-- this is what I would expect but doesn't have any effect
    [JsonProperty("patient_birthdate")] public string? PatientDateOfBirth { get; set; }
}

推荐答案

多亏了那answer个人才找到了解决方案.

我通过NuGet添加了附加的包"Swashuckle.AspNetCore.Annotation",并在我配置Swagger生成的Startup.cs中启用了注释:

services.AddSwaggerGen(c =>
    ...
    c.EnableAnnotations();
}

在我的ViewModel中,我现在可以使用以下注释:

/// <example>20.01.2002</example>
[JsonProperty("patient_birthdate"), SwaggerSchema(Format = "dd.mm.yyyy")] public string? PatientDateOfBirth { get; set; }

..这将产生一份漂亮的文档:

enter image description here

Asp.net相关问答推荐

如何在 asp.net 服务器中使用字体

WebResource.axd 上的填充无效且无法删除异常

Server.Transfer 在执行子请求时抛出错误.如何解决?

有没有一种简单的方法可以将对象属性转换为字典

使用 jQuery 验证插件匹配两个字段

使用 IIS 的 ASP.NET 调试超时

对布尔查询字符串参数使用true或1

配置转换和TransformXml 任务失败错误消息

为什么我不能在 ASP.net MVC 中使用服务器控件?

Asp.net Identity 密码哈希

发布网站项目时临时路径太长

.NET AJAX 调用 ASMX 或 ASPX 或 ASHX?

SignalR /signalr/hubs 404 未找到

如何在 asp.net 中更改 DataBinder.Eval 的日期格式?

Asp.Net web 服务:我想返回错误 403 禁止

ASP.NET MVC 路由的无限 URL 参数

使用 Twitter Bootstrap 在 @html.actionlink 中显示 html

URL 路由、图像处理程序和潜在危险的 Request.Path 值

System.Web.Caching 还是 System.Runtime.Caching 更适合 .NET 4 Web 应用程序

如何使 URL 重写与 web.Release.config 转换一起工作?