zijian666.WebApi
2.0.3-beta
dotnet add package zijian666.WebApi --version 2.0.3-beta
NuGet\Install-Package zijian666.WebApi -Version 2.0.3-beta
<PackageReference Include="zijian666.WebApi" Version="2.0.3-beta" />
<PackageVersion Include="zijian666.WebApi" Version="2.0.3-beta" />
<PackageReference Include="zijian666.WebApi" />
paket add zijian666.WebApi --version 2.0.3-beta
#r "nuget: zijian666.WebApi, 2.0.3-beta"
#:package zijian666.WebApi@2.0.3-beta
#addin nuget:?package=zijian666.WebApi&version=2.0.3-beta&prerelease
#tool nuget:?package=zijian666.WebApi&version=2.0.3-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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Microsoft.AspNetCore.ResponseCompression (>= 2.3.0)
- Swashbuckle.AspNetCore.Annotations (>= 6.4.0)
- Swashbuckle.AspNetCore.SwaggerGen (>= 6.4.0)
- Swashbuckle.AspNetCore.SwaggerUI (>= 6.4.0)
- zijian666.Converts (>= 5.4.0.3)
- zijian666.Converts.Json (>= 5.4.0.3)
- zijian666.Core.Abstractions (>= 2.0.6.2)
- zijian666.DI (>= 1.4.1)
- zijian666.DI.Autowired (>= 1.4.1)
- zijian666.DI.Configuration (>= 1.4.1)
- zijian666.WebApi.Abstractions (>= 2.0.3-beta)
-
net7.0
- Microsoft.AspNetCore.ResponseCompression (>= 2.3.0)
- Swashbuckle.AspNetCore.Annotations (>= 6.4.0)
- Swashbuckle.AspNetCore.SwaggerGen (>= 6.4.0)
- Swashbuckle.AspNetCore.SwaggerUI (>= 6.4.0)
- zijian666.Converts (>= 5.4.0.3)
- zijian666.Converts.Json (>= 5.4.0.3)
- zijian666.Core.Abstractions (>= 2.0.6.2)
- zijian666.DI (>= 1.4.1)
- zijian666.DI.Autowired (>= 1.4.1)
- zijian666.DI.Configuration (>= 1.4.1)
- zijian666.WebApi.Abstractions (>= 2.0.3-beta)
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.3-beta | 117 | 10/20/2025 |
UPLOGS.md