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 和实体框架 - 仅将实体框架用于 ORM

.Net Framework:w3wp.exe 中的异常

此版本的 SQL Server 不支持用户实例登录标志.连接将关闭

带有模型的 mvc 上传文件 - 第二个参数发布的文件为空

如何获取已构建、编码的 ViewState 的值?

Dotnet core 2.0 身份验证多模式身份 cookie 和 jwt

取消选中时 ASP.NET CheckBox 不会触发 CheckedChanged 事件

使用 JSON.NET 解析 json 字符串

在不知道键名的情况下访问 JSON 对象的元素

更改后未应用 CSS

获取页面上特定类型的所有 Web 控件

将数据表导出到 Excel 文件

aspnet_compiler 发现错误版本的 System.Web.WebPages 1.0.0.0 而不是 2.0.0.0

无法在 IIS 中启动网站 - W3SVC 正在运行

使用 VirtualPathProvider 从 DLL 加载 ASP.NET MVC 视图

路由模板分隔符/不能连续出现 - 属性路由问题

如何response.write bytearray?

好的 ASP.NET 类似 excel 的网格控件?

在 ASP.NET Web API 2 中禁用 *all* 异常处理(为我自己腾出空间)?

Appdomain 回收究竟是什么