zijian666.WebApiExtensions 1.2.0-beta

This is a prerelease version of zijian666.WebApiExtensions.
dotnet add package zijian666.WebApiExtensions --version 1.2.0-beta                
NuGet\Install-Package zijian666.WebApiExtensions -Version 1.2.0-beta                
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="zijian666.WebApiExtensions" Version="1.2.0-beta" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add zijian666.WebApiExtensions --version 1.2.0-beta                
#r "nuget: zijian666.WebApiExtensions, 1.2.0-beta"                
#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 zijian666.WebApiExtensions as a Cake Addin
#addin nuget:?package=zijian666.WebApiExtensions&version=1.2.0-beta&prerelease

// Install zijian666.WebApiExtensions as a Cake Tool
#tool nuget:?package=zijian666.WebApiExtensions&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
  1. 调用HttpContext.TraceIdentifier 可获 RequestId
  2. 默认标准返回值会返回 RequestId 属性

源码

2. AddTracingHttpClient

在操作HttpClient时,将HttpContext.TraceIdentifier加入到请求头X-Correlation-ID

当请求头已经存在X-Correlation-IDX-Request-ID时,不会覆盖已有的值

源码

3. EnableJsonStandardized

将格式化JSON功能标准化

  1. 命名方式改为小于+下划线 如 RequestId → request_id
  2. 时间格式固定为 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.2.0-beta 35 12/20/2024
1.1.14.3-beta 45 12/9/2024
1.1.14-beta 43 11/29/2024
1.1.13.6-beta 48 11/22/2024
1.1.13.2-beta 46 11/20/2024
1.1.13.1-beta 53 10/31/2024
1.1.13-beta 45 10/25/2024
1.1.10.18-beta 52 9/23/2024
1.1.10.13-beta 71 8/13/2024
1.1.10.9-beta 70 7/16/2024
1.1.10.8-beta 74 7/9/2024
1.1.10.7-beta 73 7/2/2024
1.1.10.6-beta 59 6/28/2024
1.1.10.3-beta 58 6/28/2024
1.1.10.2-beta 53 6/27/2024
1.1.10.1-beta 62 6/26/2024
1.1.10-beta 56 6/25/2024
1.1.8-beta 63 6/14/2024
1.1.7-beta 59 3/12/2024
1.1.6-beta 62 3/4/2024
1.1.5-beta 71 12/27/2023
1.1.4-beta 127 10/15/2023
1.1.3-beta 111 8/2/2023
1.1.2-beta 106 7/23/2023
1.1.1-beta 95 7/20/2023
1.0.1.2-beta 91 7/11/2023
0.0.1-dev 134 6/10/2022
0.0.0.9-dev 135 5/31/2022
0.0.0.8-dev 149 4/20/2022
0.0.0.7-dev 153 1/12/2022
0.0.0.6-dev 144 1/11/2022
0.0.0.5-dev 179 1/7/2022

UPLOGS.md