swagger web api 学习总结 part1


Part1 记录mvc api 简单使用swagger


1、创建mvc api 项目


2、初始化包 (工具-NuGet包管理器-管理解决方案的NuGet包) -Swashbuckle


3、在App_Start 生成SwaggerConfig.cs 文件   修改SwaggerConfig 文件如下 :

 public class SwaggerConfig
    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;
            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {

                        c.SingleApiVersion("v1", "API Test");
                        c.IncludeXmlComments(GetXmlCommentsPath());
                        c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                    })
                .EnableSwaggerUi(c =>
                    {

                    });
        }

        private static string GetXmlCommentsPath()
        {
            return $@"{System.AppDomain.CurrentDomain.BaseDirectory}\bin\WebAppSwagger.XML";
        }
    }

5、配置生成的xml 文件路径 也就是方法GetXmlCommentsPath() 所指定的路径


6、运行程序,地址栏输入http://localhost:2122/swagger/ui/index  成功显示


7、我们可以在此基础上扩展swagger的css、js、html 

在项目中新建个文件夹SwaggerExtensions用于存放扩展文件 ,在这个文件夹下添加一个css文件:swagger.css,我们这里只是修改了一下头部的颜色


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个使用Swagger描述API接口的Web API项目的示例: ```csharp using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; namespace MyWebApi.Controllers { [ApiController] [Route("[controller]")] public class UsersController : ControllerBase { /// <summary> /// 获取所有用户 /// </summary> /// <returns>用户列表</returns> [HttpGet] [ProducesResponseType(typeof(IEnumerable<User>), 200)] public IActionResult GetAllUsers() { return Ok(new List<User> { new User { Id = 1, Name = "Alice" }, new User { Id = 2, Name = "Bob" } }); } /// <summary> /// 获取指定用户 /// </summary> /// <param name="id">用户ID</param> /// <returns>用户信息</returns> [HttpGet("{id}")] [ProducesResponseType(typeof(User), 200)] [ProducesResponseType(404)] public IActionResult GetUser(int id) { var user = new User { Id = id, Name = "Alice" }; if (user == null) { return NotFound(); } return Ok(user); } /// <summary> /// 添加用户 /// </summary> /// <param name="user">用户信息</param> /// <returns>添加结果</returns> [HttpPost] [ProducesResponseType(typeof(User), 201)] [ProducesResponseType(400)] public IActionResult AddUser(User user) { if (user == null) { return BadRequest(); } user.Id = 3; // 模拟分配ID return CreatedAtAction(nameof(GetUser), new { id = user.Id }, user); } /// <summary> /// 更新用户 /// </summary> /// <param name="id">用户ID</param> /// <param name="user">用户信息</param> /// <returns>更新结果</returns> [HttpPut("{id}")] [ProducesResponseType(204)] [ProducesResponseType(400)] [ProducesResponseType(404)] public IActionResult UpdateUser(int id, User user) { if (user == null || user.Id != id) { return BadRequest(); } // 更新用户信息 return NoContent(); } /// <summary> /// 删除用户 /// </summary> /// <param name="id">用户ID</param> /// <returns>删除结果</returns> [HttpDelete("{id}")] [ProducesResponseType(204)] [ProducesResponseType(404)] public IActionResult DeleteUser(int id) { // 删除用户信息 return NoContent(); } } public class User { public int Id { get; set; } public string Name { get; set; } } } ``` 在上面的示例中,我们使用了 `[ApiController]` 和 `[Route]` 特性来定义控制器和路由。其中,`[ApiController]` 告诉 ASP.NET Core 这是一个 Web API 控制器,并使用默认的行为,例如自动模型绑定和验证。`[Route]` 特性指定了控制器的路由前缀。 每个操作方法都有一个 HTTP 动词特性,例如 `[HttpGet]`,指定了该方法对应的 HTTP GET 请求。我们还使用了`[ProducesResponseType]` 特性,用于指定操作方法的返回类型及其 HTTP 状态码。这些信息可以帮助 Swagger 自动生成 API 文档。 在上面的示例中,我们使用了标准的 HTTP 状态码和响应模型。但是,你也可以使用自定义的状态码和响应模型,只需在`[ProducesResponseType]` 特性中指定即可。 当你启用 Swagger 后,你可以在浏览器中访问 `http://localhost:<port>/swagger` 查看自动生成的 API 文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ycmail

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值