5

here my model for registration and set comments for example but its still not show in swagger its display somehing like this { userName:"string" }

instead of { 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; }
    }

and my method is below

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

}

but example is not shown in swagger

Jasmin Solanki
  • 369
  • 7
  • 26

2 Answers2

3

Update for Swashbuckle 4.x, which does support the use of the tag. (see https://github.com/domaindrivendev/Swashbuckle.AspNetCore )

Then my Startup.cs code looks like this

            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>();
            });
Ben Butzer
  • 915
  • 10
  • 24
1

The <example> XML documentation tag is not utilized by Swashbuckle. You have to use an IOperationalFilter to manually add examples, as there's no built-in way. However, someone was nice enough to create a NuGet package that makes it much easier, aptly named Swashbuckle.Examples. For an ASP.NET Core project, you'll actually need the Swashbuckle.AspNetCore.Examples or Swashbuckle.AspNetCore.Filters NuGets, depending on the version of Swashbuckle.AspNetCore you're running.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444