HotVol.MinimalAPIs
2.0.0
dotnet add package HotVol.MinimalAPIs --version 2.0.0
NuGet\Install-Package HotVol.MinimalAPIs -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HotVol.MinimalAPIs" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HotVol.MinimalAPIs --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HotVol.MinimalAPIs, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HotVol.MinimalAPIs as a Cake Addin #addin nuget:?package=HotVol.MinimalAPIs&version=2.0.0 // Install HotVol.MinimalAPIs as a Cake Tool #tool nuget:?package=HotVol.MinimalAPIs&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HotVol.MinimalAPIs 一个基于.NET7 MinimalAPIs封装的模块化、简单、易用、自动和配置的最小API库。
1.在Program.cs类中添加如下代码,注册模块化MinmalApi:
builder.Services.AddMinimalApis();
2.在Program.cs类中添加如下代码,使用模块化MinmalApi:
app.MapMinimalApis();
3.全局配置MinmalApi
builder.Services.AddMinimalApis(options=>{
//如下配置**仅供参考**,下面介绍所有的属性如何配置,在实际的项目中如何配置请根据项目实际情况酌情配置
//注册全局筛选器接口
options.Filters.Add<LogNamedEndpointFilter>();
options.Filters.Add<ValidateNamedEndpointFilter>();
//注册全局筛选器委托
options.HandlerFilters.Add((context, next) =>
{
return next(context);
});
//设置API所在程序集,**默认不用配置,自动加载依赖**
options.Assemblies = AppDomain.CurrentDomain.GetAssemblies();
//要求终结点在路由过程中与指定的主机之一匹配。
options.Hosts = new List<string> { "http://localhost:9001" };
//授权策略名称列表,**按照项目实际进行配置**
options.AuthorizationPolicyNames = new List<string> { "授权策略名称" };
//设置CORS策略名称,**按照项目实际进行配置**
options.CorsPolicyName = "CORS策略名称";
//设置接口返回结果缓存策略名称,**按照项目实际进行配置**
options.CacheOutputPolicyName = "接口返回结果缓存策略名称";
//设置速率限制策略名称,**按照项目实际进行配置**
options.RateLimitingPolicyName = "速率限制策略名称";
//配置自动路由部分,自动路由地址组成规则:**prefix/version/baseUrl/moduleName/ActionName/id**
//设置自动路由地址组成{id}是否追加到路由地址中去
options.RouteOptions.AutoRouteAppendId = true;
//配置自动路由匹配{id}的关键字列表
options.RouteOptions.AutoRoutePaths.Add("key");
//配置模块的后缀关键字列表
options.RouteOptions.ModuleSuffix.Add("Serever");
//配置Get请求关键字,根据方法名称与关键字列表进行匹配
options.RouteOptions.HttpGetVers.Add("Get");
//配置Post请求关键字,根据方法名称与关键字列表进行匹配
options.RouteOptions.HttpPostVers.Add("Post");
//配置Put请求关键字,根据方法名称与关键字列表进行匹配
options.RouteOptions.HttpPutVers.Add("Put");
//配置Delete请求关键字,根据方法名称与关键字列表进行匹配
options.RouteOptions.HttpDeleteVers.Add("Delete");
//配置禁用路由地址中追加prefix
options.RouteOptions.DisableRouteAppendPrefix = true;
//配置禁用路由地址中追加version
options.RouteOptions.DisableRouteAppendVersion = true;
//配置禁用路由地址中追加module
options.RouteOptions.DisableRouteAppendModule = true;
//配置禁用去掉模块的后缀
options.RouteOptions.DisableTrimModuleSuffix = true;
//配置禁用去掉方法的前缀
options.RouteOptions.DisableTrimMethodPrefix = true;
//配置禁用去掉方法的后缀关键字“Async”
options.RouteOptions.DisableTrimMethodAsyncSuffix = true;
//设置路由地址全局前缀值
options.RouteOptions.Prefix = "api";
//设置路由地址全局前缀版本值
options.RouteOptions.Version = "v1";
//配置OpenApi部分
options.OpenApiOptions.Summary = "API的中文描述";
options.OpenApiOptions.DisplayName = "API的显示别名";
options.OpenApiOptions.GroupName = "API的分组名称";
options.OpenApiOptions.Description = "API的说明";
options.OpenApiOptions.MetaData = new object[] { "API的元数据" };
options.OpenApiOptions.Tags = new string[] { "HOME", "" };
options.OpenApiOptions.IncludeOpenApi = true; // 暴露API
});
/// <summary>
/// 日志筛选器
/// </summary>
public sealed class LogNamedEndpointFilter : INamedEndpointFilter
{
public string Name => nameof(LogNamedEndpointFilter);
public ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
var result = next(context);
// 在这里做点啥
return result;
}
}
/// <summary>
/// 校验筛选器
/// </summary>
public sealed class ValidateNamedEndpointFilter : INamedEndpointFilter
{
public string Name => nameof(ValidateNamedEndpointFilter);
public ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
var result = next(context);
// 在这里做点啥
return result;
}
}
4.局部配置MinmalApi
public class HomeAPI:MinimalApi
{
public HomeAPI()
{
//如下配置**仅供参考**,下面展示所有配置说明,实际的项目中如何配置请根据项目实际情况酌情配置
this.WithDisableGlobalApiOptions() // 当前模块,禁用全局配置,使用默认配置
.WithIgnoreEndpointFilters(nameof(LogNamedEndpointFilter)) // 当前模块,忽略全局筛选器LogNamedEndpointFilter
.WithFilters(filters =>
{
filters.Add((context, next) =>
{
return next(context);
});
}) // 当前模块,新增一个筛选器委托
.WithDisableTrimModuleSuffix() // 当前模块,禁用去掉模块的后缀
.WithDisableTrimMethodPrefix() // 当前模块,禁用去掉方法的后缀
.WithDisableTrimMethodAsyncSuffix()// 当前模块,禁用去掉方法的“Async”后缀
.WithRouteDisableAppendPrefix()// 当前模块,路由地址不追加Perfix
.WithRouteDisableAppendVersion()// 当前模块,路由地址不追加Version
.WitheRouteDisableAppendModule()// 当前模块,路由地址不追加模块名称
.WitheRouteEnableAppendId() //当前模块,路由地址追加{Id}参数
.WithRoutePrefix("api/wts")// 设置当前模块,全局前缀
.WithRouteVersion("v2")// 设置当前模块,全局前缀
.WithRouteIds("TKey") // 设置当前模块,路由{Id}参数
.WithRouteHttpGetVers("GL", "Qr")// 设置当前模块,方法名称与GET请求匹配关键字
.WithRouteHttpPostVers("Save", "Co")// 设置当前模块,方法名称与Post请求匹配关键字
.WithRouteHttpPutVers("Mf", "Ut")// 设置当前模块,方法名称与Put请求匹配关键字
.WithRouteHttpDeleteVers("Del")// 设置当前模块,方法名称与Delete请求匹配关键字
.WithOpenApiSummary("")// 设置当前模块,OpenApi
.WithOpenApiGroupName("")// 设置当前模块,OpenApi
.WithOpenApiDisplayName("")// 设置当前模块,OpenApi
.WithOpenApiDescription("")// 设置当前模块,OpenApi
.WithOpenApiMetadata("", 1)// 设置当前模块,OpenApi
.WithOpenApiTags("Home", "Default")// 设置当前模块,OpenApi
;
}
}
5.简单示例:
/// <summary>
/// 简单的模块化MinimalApi
/// </summary>
public class HomeAPI : MinimalApi
{
public HomeAPI()
{
this.WithDisableGlobalApiOptions();// 配置当前模块不使用全局配置,使用默认的配置
}
/// <summary>
/// 方式一:手动注册路由以及路由处理程序
/// </summary>
/// <param name="app">路由终结点构建对象</param>
/// <returns>路由终结点集合</returns>
public override IEnumerable<IEndpointConventionBuilder> AddRoutes(IEndpointRouteBuilder app)
{
return new[]
{
app.MapGet("/Test1", () => $"{DateTime.Now}"),
app.MapGet("/Test2", GetTest2),
};
}
/// <summary>
/// 方式1:方法被手动注册路由,访问修饰符必须是非public
/// </summary>
/// <returns></returns>
private string GetTest2() => $"{DateTime.Now}";
/// <summary>
/// 方式2:方法被自动注册路由处理程序,路由为:[perfix]/[version]/[baseUrl]/[module]/[action]/[id?],每个[]内的元素为IsNullOrWhiteSpace时,将被忽略,但是[module]/[action]是必须的
/// </summary>
/// <returns></returns>
public string GetTest3() => $"{DateTime.Now}";
}
6.完整示例:
using HotVol.MinimalAPIs;
var builder = WebApplication.CreateBuilder(args);
//1.添加应用程序所需的所有服务
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();//添加Swagger相关服务
builder.Services.AddMinimalApis();//注册MinimalApis
var app = builder.Build();
//2.配置Http请求管道
if (app.Environment.IsDevelopment()) // 判断是否为开发环境
{
app.UseSwagger(); //使用Swagger中间件
app.UseSwaggerUI();//使用Swagger中间件静态文件
}
app.MapMinimalApis();//使用MinimalApis
// 终结点中间件
app.Run();
//定义API
public class HomeAPIA : MinimalApi
{
public HomeAPIA()
{
}
public DateTime GetDateTime()
{
return DateTime.Now;
}
public DateTime? GetDateTimeNull()
{
return DateTime.Now;
}
public string GetString()
{
return DateTime.Now.ToString();
}
public string GetNum()
{
return DateTime.Now.ToString();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net7.0
- FluentValidation (>= 11.5.2)
- Microsoft.AspNetCore.OpenApi (>= 7.0.5)
- Microsoft.Extensions.DependencyModel (>= 7.0.0)
- Swashbuckle.AspNetCore.SwaggerGen (>= 6.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.0 | 200 | 5/11/2023 |