DuMes.Expansion.WebApi 1.1.7

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

// Install DuMes.Expansion.WebApi as a Cake Tool
#tool nuget:?package=DuMes.Expansion.WebApi&version=1.1.7                

DuMes.Expansion.WebApi

介绍

此组件是一个用于扩展WebApi的组件。

包含语言、跨域、Swagger、WebApi以简化WebApi搭建时间

使用说明
  1. 注册语言
//注册语言扩展
builder.Services.AddLanguageExtension(["zh-CN", "en"]);
//使用国际化扩展
app.UseLanguageExtension();
  1. 注册跨域
//注册跨域
const string policyName = "DefaultPolicy";
var crossOriginUrls = builder.Configuration.GetSection("CrossOriginUrls").Get<string[]>();
builder.Services.AddCrossOriginExtension(policyName, crossOriginUrls);

//使用跨域
app.UseCors(policyName);
  1. 注册Swagger
//注册Swagger扩展
var swaggerDocument = typeof(SwaggerDocument).GetConstantInClass();
builder.Services.AddSwaggerExtension(swaggerDocument);

//使用Swagger扩展
app.UseSwaggerExtension(swaggerDocument);
  1. 注册WebApi
//注册WebApi扩展
builder.Services.AddWebApiExtension([typeof(WebApiSessionFilter)], jsonOptions =>
    {
        //指定格式化方式
        jsonOptions.SerializerSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
        //返回的日期格式化
        jsonOptions.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
        //返回的结果设置默认序列化
        jsonOptions.SerializerSettings.ContractResolver = new DefaultContractResolver();
    },
    exceptionHandler => { exceptionHandler.AddExceptionHandler<GlobalExceptionHandler>(); },
    builder.Configuration.GetSection("JwtConfig").Get<JwtConfig>(),
    builder.Configuration.GetSection("RedisConfig").Get<JwtRedisConfig>());

//使用WebAPI错误处理程序
app.UseExceptionHandler();
{
  //Jwt配置
  "JwtConfig": {
    "SecurityKey": "0559D4D5-173A-96A1-AC01-FBC1374D6205",
    "Issuer": "SiaSun",
    "Audience": "SiaSun",
    //默认token超时时间(分钟)
    "Expiration": 1440
  },
  //Redis配置
  "RedisConfig": {
    "Host": "101.35.113.125",
    "Port":6379,
    "Password": "siasun@sh",
    "Db": 4,
    "Prefix": "Test_",
    "UseSsl": false
  }
}

使用WebApiResult返回的Json对象格式为

{
    "data": {
        "Name": "小红",
        "Age": 18
    },
    "isSuccess": true,
    "errorMessage": null
}

类中开头为小写,data内部为原始大小写

如当使用自带的WebApiSessionFilter过滤器时,接口一定要添加上[ValidatePermission([])]特性 ,完整API配置如下:

/// <summary>
///     获取账号
/// </summary>
/// <returns></returns>
[HttpGet("GetAccount")]
[ValidatePermission(["add"])]
public async Task<WebApiResult<string>> GetAccount()
{
    await Task.Delay(1);
    return WebApiSession.Account.Value.Id;
}

使用jwt时,webapi扩展JwtExpansion会自动注入IOC容器 JwtExpansion可以生成和解析token

//账号授权对象格式为
public record AccountAuthorize(string Id, string Ip, string LoginType, List<string> RoleIdList, DateTime ExpirationTime, string Token);

过滤器错误会返回

验证权限属性未配置  HTTP Code: 501
令牌验证错误  HTTP Code: 400
登录已过期,请重新登录  HTTP Code: 401
接口权限错误  HTTP Code: 401
未知错误  HTTP Code: 500

windows service部署添加

var options = new WebApplicationOptions
{
    Args = args,
    ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};

var builder = WebApplication.CreateBuilder(options);

if (OperatingSystem.IsWindows() && WindowsServiceHelpers.IsWindowsService())
    //增加windows服务
    builder.Host.UseWindowsService();
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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.1.7 94 12/14/2024
1.1.6 72 12/14/2024
1.1.5 82 12/14/2024
1.1.4 90 12/12/2024
1.1.3 74 12/10/2024
1.1.2 93 11/28/2024
1.1.1 89 11/27/2024
1.1.0 83 11/27/2024
1.0.9 107 11/25/2024
1.0.8 100 11/25/2024
1.0.7 93 11/25/2024
1.0.6 98 11/25/2024
1.0.5 99 11/25/2024
1.0.4 99 11/25/2024
1.0.3 107 11/25/2024
1.0.2 100 11/25/2024
1.0.1 101 11/25/2024
1.0.0 125 11/21/2024 1.0.0 is deprecated because it is no longer maintained and has critical bugs.