很久不来写blog了,换了新工作后很累,很忙。每天常态化加班到21点,偶尔还会到凌晨,加班很累,但这段时间,也确实学到了不少知识,今天这篇文章和大家分享一下:Asp.Net Core中使用Swagger,你不得不踩的坑.

第一步:搭建一个webapi项目或者mvc项目,引入swagger nuget

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

第二部:引入版本控制nuget,添加关键性配置代码

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

services.AddSwaggerGen(
options =>
{
// resolve the IApiVersionDescriptionProvider service
// note: that we have to build a temporary service provider here because one has not been created yet
var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>(); // add a swagger document for each discovered API version
// note: you might choose to skip or document deprecated API versions differently
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));
} // add a custom operation filter which sets default values
options.OperationFilter<SwaggerDefaultValues>(); //
// integrate xml comments
options.IncludeXmlComments(XmlCommentsFilePath);
options.IncludeXmlComments(XmlModelsFilePath);
}); public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApiVersionDescriptionProvider provider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseMvc(); //Swagger
app.UseSwagger();
app.UseSwaggerUI(
options =>
{
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}
});
}

第三部:新建控制器,测试我们的项目

namespace TB.AspNetCore.Swagger.SApi.Controllers
{
[Produces("application/json")]
[ApiVersion("1.0", Deprecated = false)]
[Route("api/v{api-version:apiVersion}/[controller]/[action]")]
[ApiController]
public class OrderController : ControllerBase
{
/// <summary>
/// 查询订单
/// </summary>
/// <param name="orderCode">订单号</param>
/// <param name="orderName">订单名称</param>
/// <returns></returns>
[HttpGet("{orderCode}/{orderName}")]
public OrderModel QueryOrder(string orderCode, string orderName)
{
OrderModel model = new OrderModel
{ OrderCode = orderCode };
return model;
} /// <summary>
/// 提交订单
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public OrderModel SubOrder([FromBody]OrderModel model)
{
return model;
}
} public class OrderModel
{
/// <summary>
/// 订单号
/// </summary>
public string OrderCode { get; set; } /// <summary>
/// 订单金额
/// </summary>
public decimal OrderAmount { get; set; } /// <summary>
/// 订单类型
/// <Remark>
/// 0 商家入驻
/// 1 线下交易
/// </Remark>
/// </summary>
public OrderTypeInfo OrderType { get; set; } } /// <summary>
///
/// </summary>
public enum OrderTypeInfo
{
/// <summary>
/// 商家入驻
/// </summary>
[Description("商家入驻")]
StoreEntry = ,
/// <summary>
/// 线下交易
/// </summary>
[Description("线下交易")]
StoreTrade = , }
}

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

欢迎大家关注我都我的微信 公众号,公众号涨粉丝人数,就是你们对我的喜爱程度!

Asp.Net Core中使用Swagger,你不得不踩的坑-LMLPHP

04-20 10:26