我正在尝试在 ASP.NET 样板框架中进行版本控制。
我在 Swagger Gen 中创建了两个版本(“v1.0”和“v2.0”)并为 Web API 设置了 API 版本,但每次我从 Swagger 获得两个版本的所有 API 时。
启动.cs:
AddSwaggerGen
在ConfigureServices()
:
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1.0", new Info { Title = "My API", Version = "v1.0" });
options.SwaggerDoc("v2.0", new Info { Title = "My API", Version = "v2.0" });
options.DocInclusionPredicate((docName, description) => true);
// Define the BearerAuth scheme that's in use
options.AddSecurityDefinition("bearerAuth", new ApiKeyScheme()
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = "header",
Type = "apiKey"
});
// Assign scope requirements to operations based on AuthorizeAttribute
options.OperationFilter<SecurityRequirementsOperationFilter>();
});
UseSwaggerUI
在配置()中:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseAbp(options => { options.UseAbpRequestLocalization = false; });
app.UseCors(_defaultCorsPolicyName);
app.UseStaticFiles();
app.UseAuthentication();
app.UseAbpRequestLocalization();
app.UseSignalR(routes =>
{
routes.MapHub<AbpCommonHub>("/signalr");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "defaultWithArea",
template: "{area}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.InjectOnCompleteJavaScript("/swagger/ui/abp.js");
options.InjectOnCompleteJavaScript("/swagger/ui/on-complete.js");
options.SwaggerEndpoint("/swagger/v1.0/swagger.json", "My API V1.0");
options.SwaggerEndpoint("/swagger/v2.0/swagger.json", "My API V2.0");
});
}
API 控制器 - v1.0:
[ApiVersion("v1.0")]
[Route("/api/invoicemodule/1.0/[controller]")]
public class InvoiceController : MyControllerBase
{
[HttpGet, MapToApiVersion("v1.0")]
public IActionResult GetInvoiceById(string invoiceid)
{
//BusinessService.SparePartHistoryService sas = new BusinessService.SparePartHistoryService(_logger, _localizer, _configuration);
if (string.IsNullOrEmpty(invoiceid)) return BadRequest("'Id' cannot be null or empty.");
try
{
BusinessModels.Invoice sp = new BusinessModels.Invoice
{
Id = ""
};
if (sp != null)
{
return Ok(sp);
}
else
{
return NotFound();
}
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
}
API 控制器 - v2.0:
[ApiVersion("v2.0")]
[Route("/api/invoicemodule/2.0/[controller]")]
public class InvoiceController : MyControllerBase
{
[HttpGet, MapToApiVersion("v2.0")]
public IActionResult GetInvoiceById(string invoiceid)
{
//BusinessService.SparePartHistoryService sas = new BusinessService.SparePartHistoryService(_logger, _localizer, _configuration);
if (string.IsNullOrEmpty(invoiceid)) return BadRequest("'Id' cannot be null or empty.");
try
{
BusinessModels.Invoice sp = new BusinessModels.Invoice
{
Id = ""
};
if (sp != null)
{
return Ok(sp);
}
else
{
return NotFound();
}
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
}