例如,这里是我的注册模型和设置注释,但它仍然没有以大张旗鼓的方式显示,它的显示是这样的
{
用户名:“字符串”
}
代替
{
用户名:“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)
{
}
但示例并未以 swagger 形式显示
Swashbuckle 4.x 的更新,支持使用标签。 (看https://github.com/domaindrivendev/Swashbuckle.AspNetCore 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>();
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)