seo如何优化网站推广,农林科技公司网站模板,贵阳建设网站公司,php网站颜色改变应用是演进的#xff0c;通常我们用版本号来管理。api也是演进的#xff0c;这篇博文就说说asp.net web api演进时的版本管理。asp.net web api的版本管理是通过微软的一个包来实现的。Install-Package Microsoft.AspNetCore.Mvc.Versioning通过url参数: api/order/api-versi… 应用是演进的通常我们用版本号来管理。api也是演进的这篇博文就说说asp.net web api演进时的版本管理。asp.net web api的版本管理是通过微软的一个包来实现的。Install-Package Microsoft.AspNetCore.Mvc.Versioning通过url参数: api/order/api-version2.0在startup中注入ApiVersion public void ConfigureServices(IServiceCollection services){services.AddApiVersioning();services.AddControllers();}
在ProductController中标注版本特性using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace APIVersionDemo.Controllers
{[ApiController][Route(api/[controller])][ApiVersion(1.0, Deprecated true)][ApiVersion(2.0)]public class ProductController : ControllerBase{private readonly ILoggerProductController _logger;public ProductController(ILoggerProductController logger){_logger logger;}//1.0的api[HttpGet({id})]public Product QueryProduct([FromRoute] int id){_logger.LogInformation(v1.0查询产品);return new Product() { ID id, Name A物品, Price 100.20m };}//2.0的api[HttpGet({id})][MapToApiVersion(2.0)]public Product QueryProductv2([FromRoute] int id){_logger.LogInformation(v2.0查询产品);return new Product2() { ID id, Name A物品, Price 100.20m, Description 产自山西 };} }/// summary/// 1.0的产品类/// /summarypublic class Product{public int ID { get; set; }public string Name { get; set; }public decimal Price { get; set; }}/// summary/// 2.0的产品类/// /summarypublic class Product2 : Product{public string Description { get; set; }}
}
通过http://localhost:5000/api/product/1?api-version2.0方式访问不同的版本。显而易见这种通过在请求url末尾加参数的方式有点啰嗦。通过MediaType{Accept:application/json;version2.0}或header{version:2.0}修改startup的apiversion注入参数 public void ConfigureServices(IServiceCollection services){services.AddApiVersioning(opt {opt.AssumeDefaultVersionWhenUnspecified true;opt.DefaultApiVersion new ApiVersion(1, 0);opt.ApiVersionReader ApiVersionReader.Combine(new MediaTypeApiVersionReader(version),new HeaderApiVersionReader(api-version));opt.ReportApiVersions true;});services.AddControllers();}
ProductController不变。请求http://localhost:5000/api/product/1,header参数可以适配两种方式Accept : application/json;version2.0api-version : 2.0这样方便客户端请求的整体处理。通过url:api/v2.0/orderstrartup.cs注入apiversion public void ConfigureServices(IServiceCollection services){services.AddApiVersioning();services.AddControllers();}
这里引入OrderController在Route特性上改变url在url里增加版本信息{version:apiVersion}using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace APIVersionDemo.Controllers
{[ApiController][Route(api/v{version:apiVersion}/[controller])][ApiVersion(1.0, Deprecated true)][ApiVersion(2.0)]public class OrderController : ControllerBase{private readonly ILoggerOrderController _logger;public OrderController(ILoggerOrderController logger){_logger logger;}//v1订单api[HttpGet({id})]public Order QueryOrder(){_logger.LogInformation(v1查询产品);return new Order(){OrderID 1,Products new ListProduct(){new Product() { ID 1, Name A物品, Price 100.20m }}};}//v2订单api[HttpGet({id})][MapToApiVersion(2.0)]public Order2 QueryOrder2(){_logger.LogInformation(v2查询产品);return new Order2(){OrderID 1,Products new ListProduct2(){new Product2() { ID 1, Name A物品, Price 100.20m, Description 产自山西 }}};}}//v1订单类public class Order{public int OrderID { get; set; }public ListProduct Products { get; set; }}//v2订单类public class Order2{public int OrderID { get; set; }public ListProduct2 Products { get; set; }}
}
这时请求就变成http://localhost:5000/api/v2.0/order了可以通过不同的url来访问不同的api版本这种方式也有利于客户端统一配置baseurl来切换请求api版本。最后apiversion这个包还带来了一个无侵入controller的方式来配置api的版本本质与加在Controller上的特性信息是一致的——附加版本信息和对应关系。services.AddApiVersioning(opt
{ opt.Conventions.ControllerProductController().HasApiVersion(2, 0).HasDeprecatedApiVersion(1, 0).Action(typeof(ProductController).GetMethod(nameof(ProductController.QueryProductv2))!).MapToApiVersion(2, 0);
});