webapi 将注释显示在swagger上

1. 再Program.cs 文件中添加
builder.Services.AddSwaggerGen(options =>
{
    //读取根据api生成的控制器xml文件
    var file = Path.Combine(AppContext.BaseDirectory, "WebApplication1.Webapi.xml");
    //显示控制器层注释
    options.IncludeXmlComments(file, true);
    //对action进行排序
    options.OrderActionsBy(r => r.RelativePath);
});

2. 

 3. 

 4, 使用

 Users.cs 文件内容

namespace WebApplication1.Webapi.Model
{
    public class Users
    {
        /// <summary>
        /// 用户id
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 用户名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 电子邮箱
        /// </summary>
        public string Email { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }
        /// <summary>
        /// 昵称
        /// </summary>
        public string Username { get; set; }
    }
}

 

 5, 运行项目

 

 

 

posted @ 2024-06-02 19:41  龙卷风吹毁停车场  阅读(58)  评论(0)    收藏  举报