zijian666.WebApiExtensions.Abstractions 1.2.0-beta

This is a prerelease version of zijian666.WebApiExtensions.Abstractions.
dotnet add package zijian666.WebApiExtensions.Abstractions --version 1.2.0-beta                
NuGet\Install-Package zijian666.WebApiExtensions.Abstractions -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.Abstractions" 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.Abstractions --version 1.2.0-beta                
#r "nuget: zijian666.WebApiExtensions.Abstractions, 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.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
  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.
  • 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