zijian666.WebApi.Rewriter 2.2.1-beta

This is a prerelease version of zijian666.WebApi.Rewriter.
dotnet add package zijian666.WebApi.Rewriter --version 2.2.1-beta
                    
NuGet\Install-Package zijian666.WebApi.Rewriter -Version 2.2.1-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.WebApi.Rewriter" Version="2.2.1-beta" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="zijian666.WebApi.Rewriter" Version="2.2.1-beta" />
                    
Directory.Packages.props
<PackageReference Include="zijian666.WebApi.Rewriter" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add zijian666.WebApi.Rewriter --version 2.2.1-beta
                    
#r "nuget: zijian666.WebApi.Rewriter, 2.2.1-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.
#:package zijian666.WebApi.Rewriter@2.2.1-beta
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=zijian666.WebApi.Rewriter&version=2.2.1-beta&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=zijian666.WebApi.Rewriter&version=2.2.1-beta&prerelease
                    
Install as a Cake Tool

Rewriter URL 重写和重定向模块

概述

Rewriter 是一个 URL 重写和重定向功能模块,基于 ASP.NET Core 的 RewriteMiddleware,支持通过正则表达式配置 URL 重写和重定向规则。

快速开始

1. 注册服务

using zijian666.WebApi;

var builder = WebApplication.CreateBuilder(args);

// 添加 Rewriter 功能
builder.Services.AddWebApiFeature<RewriterBuilder>(rewriter =>
{
    // 添加重写规则
    rewriter.AddRewriter(new Dictionary<string, string>
    {
        ["^/old/(.*)$"] = "/new/$1",
    });
    
    // 添加重定向规则
    rewriter.AddRedirect(new Dictionary<string, string>
    {
        ["^/old-page$"] = "/new-page"
    });
});

var app = builder.Build();

// 启用 Rewriter 中间件
app.UseWebApiFeature<RewriterBuilder>();

app.Run();

2. 使用 Configure 方法

builder.Services.AddWebApiFeature<RewriterBuilder>(rewriter =>
{
    rewriter.Configure(options =>
    {
        // 使用 ASP.NET Core 原生的 RewriteOptions API
        options.AddRedirect("^/old$", "/new", 301);
        options.AddRewrite("^/api/(.*)$", "/api/v2/$1", skipRemainingRules: false);
    });
});

配置说明

AddRewriter

添加 URL 重写规则,请求路径会被内部重写,客户端不会感知:

rewriter.AddRewriter(new Dictionary<string, string>
{
    ["^/api/(.*)$"] = "/api/v2/$1"  // 将 /api/xxx 重写为 /api/v2/xxx
});

AddRedirect

添加 URL 重定向规则,客户端会收到 301 重定向响应:

rewriter.AddRedirect(new Dictionary<string, string>
{
    ["^/old$"] = "/new"  // 访问 /old 会重定向到 /new
});

Configure

直接配置 RewriteOptions,可以使用所有 ASP.NET Core 原生的重写功能:

rewriter.Configure(options =>
{
    options.AddRedirect("^/old$", "/new", 301);
    options.AddRewrite("^/api/(.*)$", "/api/v2/$1", skipRemainingRules: false);
    // 可以使用所有 RewriteOptions 的方法
});

工作原理

  1. 服务注册阶段(AddService):

    • 收集所有配置的规则
    • 通过 services.Configure<RewriteOptions> 注册配置
  2. 中间件启用阶段(UseService):

    • 调用 app.UseRewriter() 启用重写中间件
    • 中间件从 DI 容器中获取配置的 RewriteOptions
  3. 请求处理阶段:

    • 按配置的规则顺序匹配请求路径
    • 重写规则:内部修改请求路径,继续处理
    • 重定向规则:返回 301 响应,包含 Location 头

注意事项

  • 规则按添加顺序执行,匹配成功后停止后续规则检查
  • 正则表达式默认不区分大小写
  • 如果没有配置任何规则,中间件不会启用
  • 重定向规则会保留查询字符串
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.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on zijian666.WebApi.Rewriter:

Package Downloads
zijian666.WebApi

用于快速创建简单易用的标准化WebApi项目

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.2.1-beta 103 5/31/2026

UPLOGS.md