zijian666.WebApiExtensions.Abstractions
1.2.0-beta
dotnet add package zijian666.WebApiExtensions.Abstractions --version 1.2.0-beta
NuGet\Install-Package zijian666.WebApiExtensions.Abstractions -Version 1.2.0-beta
<PackageReference Include="zijian666.WebApiExtensions.Abstractions" Version="1.2.0-beta" />
paket add zijian666.WebApiExtensions.Abstractions --version 1.2.0-beta
#r "nuget: zijian666.WebApiExtensions.Abstractions, 1.2.0-beta"
// Install zijian666.WebApiExtensions.Abstractions as a Cake Addin #addin nuget:?package=zijian666.WebApiExtensions.Abstractions&version=1.2.0-beta&prerelease // Install zijian666.WebApiExtensions.Abstractions as a Cake Tool #tool nuget:?package=zijian666.WebApiExtensions.Abstractions&version=1.2.0-beta&prerelease
WebApi 脚手架
介绍
快速搭建WebApi应用
更新日志
安装
nuget - zijian666.WebApiExtensions
项目示例
使用说明
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddWebApiOptions(option => {
// 设置需要开启的功能
option.EnableTraceIdentifier();
option.AddTraceableHttpClient("");
option.EnableJsonStandardized();
option.EnableCors();
option.EnableRequestBuffering("api/test/hello");
option.EnableSwaggerGen();
option.EnableResultStandardized();
option.BindConfigurationSection();
option.AddInterfacesAsControllers();
});
}
或使用配置文件appsettings.json
{
"WebApi": {
"*": true,
"EnableRequestBuffering": false,
"EnableTraceIdentifier": true,
"AddTraceableHttpClient": true,
"EnableJsonStandardized": true,
"EnableCors": true,
"EnableSwaggerGen": true,
"EnableResultStandardized": true,
"BindConfigurationSection": true,
"AddInterfacesAsControllers": [
{
"namespace": "InOpsManager.Service.Abstractions",
"routetemplate": "123"
}
]
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddWebApiOptions(Configuration);
}
功能说明
1. EnableTraceIdentifier
- 调用
HttpContext.TraceIdentifier
可获 RequestId - 默认标准返回值会返回
RequestId
属性
2. AddTracingHttpClient
在操作HttpClient
时,将HttpContext.TraceIdentifier
加入到请求头X-Correlation-ID
当请求头已经存在
X-Correlation-ID
或X-Request-ID
时,不会覆盖已有的值
3. EnableJsonStandardized
将格式化JSON功能标准化
- 命名方式改为小于+下划线 如 RequestId → request_id
- 时间格式固定为 yyyy-MM-dd HH:mm:ss
4. EnableCors
所有WEBAPI支持跨域
5. EnableRequestBuffering
所有请求会将请求正文缓冲到内存中,以便请求体可被多次读取
详见: HttpRequestRewindExtensions.EnableBuffering
可设置不需要缓存的例外情况情况
使用说明
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddWebApiOptions(option => {
option.EnableRequestBuffering("api/test/hello", "api/abc/[api1|api2]"); // 支持前缀匹配和正则匹配
option.EnableRequestBuffering(""api/abc/[api3]"); // 支持多次调用
});
}
6. EnableSwaggerGen
可打开/swagger/index.html
访问WebApi文档
7. EnableResultStandardized
格式化所有返回值
8. BindConfigurationSection
绑定配置类
使用说明
定义一个配置文件
{
"ali": {
"AppKey": "AppKey",
"SecretKey": "SecretKey"
}
}
定义一个配置类
[ConfigurationSection("ali")]
public class AliConfig
{
public string AppKey { get; set; }
public string SecretKey { get; set; }
}
启用BindConfigurationSection
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddWebApiOptions(option => {
option.BindConfigurationSection();
});
}
服务注入
public class TestController : ApiControllerBase
{
private readonly AliConfig _aliConfig;
public TestController(AliConfig aliConfig)
=> _aliConfig = aliConfig;
}
8. AddInterfacesAsControllers
将指定接口添加为控制器
使用说明
定义服务接口,并标记[HttpApi]
,或手动注入
默认路由 /api/[controller]/[action],可以通过设置 HttpApiAttribute.
Template
修改默认值
[HttpApi]
public interface IDemoService
{
string X_Forwarded_For { get; set; }
string Authorization { get; set; }
string Hello();
int GetNumber();
}
实现接口
略
注入
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddWebApiOptions(option => {
option.AddInterfacesAsControllers(); // 注入标记为 [HttpApi] 的所有接口
// option.AddInterfacesAsControllers("WebApiDemo.Services");// 注入指定命名空间下的所有接口
// option.AddInterfacesAsControllers(typeof(IDemoService)); // 注入需要转换为Controller的服务接口
});
}
注意事项1
无
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. 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. |
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on zijian666.WebApiExtensions.Abstractions:
Package | Downloads |
---|---|
zijian666.WebApiExtensions
用于快速创建简单易用的标准化WebApi项目 |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.2.0-beta | 35 | 12/20/2024 |
UPLOGS.md