如何通过xml文档在swagger中设置示例?

Jas*_*nki 6 asp.net swagger swagger-ui .net-core asp.net-core

例如,这里是我的注册模型和设置注释,但它仍然没有大张旗鼓地显示,它的显示类似于这样 { userName:"string" }

而不是 { userName:"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; }
    }
Run Code Online (Sandbox Code Playgroud)

我的方法如下

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

}
Run Code Online (Sandbox Code Playgroud)

但示例并未以 swagger 形式显示

Ben*_*zer 3

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 StackOverflow
                c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
                // enable the annotations on Controller classes [SwaggerTag]
                c.EnableAnnotations();
                // to allow for a header parameter
                c.OperationFilter<AddRequiredHeaderParameter>();
            });
Run Code Online (Sandbox Code Playgroud)