如何通过 xml 文档大摇大摆地树立榜样?

How to set example in swagger by xml documentation?

例如,这是我的注册模型和设置评论,但它仍然没有大摇大摆地显示,它的显示有点像这样 { 用户名:"string" }

而不是 { 用户名:"Jasmin" }

public class RegisterViewModel
    {
        /// <summary>
        /// Name of the user
        /// </summary>
        /// <example>Jasmin</example>
        [Required]
        [Display(Name = "Name")]
        public string UserName { get; set; }

        /// <summary>
        /// User Contact Number
        /// </summary>
        /// <example>9033156314</example>
        [Required]
        [Phone]
        [Display(Name = "PhoneNumber")]
        public string ContactNumber { get; set; }

        /// <summary>
        /// User Device Id
        /// </summary>
        /// <example>12364457tryhret1223</example>
        [Required]
        public string DeviceId { get; set; }

        /// <summary>
        /// User Device Info
        /// </summary>
        /// <example>Jasmin</example>
        [Required]
        public string DeviceInfo { get; set; }
    }

下面是我的方法

/// <summary>
        /// Register User Through Contact Number.
        /// </summary>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> Register([FromBody]RegisterViewModel model)
        {

}

但是例子并没有大摇大摆地展示

Swashbuckle 未使用 <example> XML 文档标签。您必须使用 IOperationalFilter 手动添加示例,因为没有内置方法。然而,有人非常好地创建了一个 NuGet 包,使它更容易,恰当地命名为 Swashbuckle.Examples。对于 ASP.NET 核心项目,您实际上需要 Swashbuckle.AspNetCore.ExamplesSwashbuckle.AspNetCore.Filters NuGet,具体取决于您 运行 的 Swashbuckle.AspNetCore 版本。

更新 Swashbuckle 4.x,它支持使用标签。 (参见 https://github.com/domaindrivendev/Swashbuckle.AspNetCore

那么我的 Startup.cs 代码如下所示

            services.AddSwaggerGen(c =>
            {
                // Set Title and version from config
                c.SwaggerDoc("v1", new Info { Title = "My Title", Version = "1.0", Description = "My Description" });
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                // pick comments from classes, include controller comments: another tip from Whosebug
                c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
                // enable the annotations on Controller classes [SwaggerTag]
                c.EnableAnnotations();
                // to allow for a header parameter
                c.OperationFilter<AddRequiredHeaderParameter>();
            });