net Core 2.1 Swagger 的使用
程序员文章站
2022-05-29 20:04:08
...
1,第一步新建一个Webapi项目:
2, 程序包管理器控制台
Install-Package Swashbuckle.AspNetCore
或是在nuget下如下操作,都可以将包导入
3,安装成功后,配置Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Swagger;
namespace WebApplication5
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
//配置第一个Doc
c.SwaggerDoc("v1", new Info { Title = "My API_1", Version = "v1" });
//配置第二个Doc
c.SwaggerDoc("v2", new Info { Title = "My API_2", Version = "v2" });
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.SwaggerEndpoint("/swagger/v2/swagger.json", "My API V2");
c.RoutePrefix = "swagger";
});
app.UseSwagger();
app.UseMvc();
}
}
}上
上面的进行简单配置后启动的效果如下:
4,设置一下默认启动(修改launchSettings)
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
5,给api添加描述与备注
(1),第一步
(2),第二步,配置xml文件
(3),添加接口描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication5.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
/// <summary>
/// Get 请求
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
/// <summary>
/// 通过Id进行Get请求
/// </summary>
/// <param name="id">编号</param>
/// <returns></returns>
// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
最后运行效果如下:
上一篇: 三种经典iPhone上网络抓包方法详解
推荐阅读
-
C#使用ADO.Net部件来访问Access数据库的方法
-
ASP.NET使用X509Certificate2出现一系列问题的解决方法
-
灵活掌握Asp.net MVC中GridView的使用方法
-
.Net Core 下使用ZKWeb.System.Drawing实现验证码功能(图形验证码)
-
详解在ASP.NET Core 中使用Cookie中间件
-
.Net core下直接执行SQL语句并生成DataTable的实现方法
-
使用ASP.NET 状态服务碰到的一些情况
-
ewebeditor在.net的使用方法
-
ASP.NET Log4Net日志的配置及使用,文件写入
-
在.Net中使用MongoDB的方法教程