asp.net-core-2.0 - 自定义网址重写

标签 asp.net-core-2.0 razor-pages

我想使用UseRewriter中间件重写一个简单的URL。

var rewrite = new RewriteOptions()
    .AddRedirect("Page1From", "Page1To")        // Redirect
    .AddRewrite("Page2From", "Page2To", true);  // Rewrite

app.UseRewriter(rewrite);

结果是,URL“/Page1From”将被重定向到“Page1To”,URL“Page2From”将显示“/Page2To”的内容而无需重定向。

我想通过使用数据库中的数据来实现AddRewrite方法,但是我只找到了如何使用自定义规则进行重定向。
var rewrite = new RewriteOptions()
    .AddRedirect("Page1From", "Page1To")        // Redirect
    .AddRewrite("Page2From", "Page2To", true);  // Rewrite
    .Add(new MoviesRedirectRule(                // Custom Rule
        matchPaths: new[] { "/Page3From1", "/Page3From2", "/Page3From3" },
        newPath: "/Page3To"));

app.UseRewriter(rewrite);

规则如下:
public class MoviesRedirectRule : IRule
{
    private readonly string[] matchPaths;
    private readonly PathString newPath;

    public MoviesRedirectRule(string[] matchPaths, string newPath)
    {
        this.matchPaths = matchPaths;
        this.newPath = new PathString(newPath);
    }

    public void ApplyRule(RewriteContext context)
    {
        var request = context.HttpContext.Request;

        // if already redirected, skip
        if (request.Path.StartsWithSegments(new PathString(this.newPath)))
        {
            return;
        }

        if (this.matchPaths.Contains(request.Path.Value))
        {
            var newLocation = $"{this.newPath}{request.QueryString}";

            var response = context.HttpContext.Response;
            response.StatusCode = StatusCodes.Status302Found;
            context.Result = RuleResult.EndResponse;
            response.Headers[HeaderNames.Location] = newLocation;
        }
    }
}

这将重定向以下URL:
  • /Page3From1
  • /Page3From2
  • /Page3From3

  • 到/Page3To

    我想创建类似的内容,但不会重定向,但我想重写该URL,以便该URL保持不变,但将显示指定URL的内容。

    有人可以告诉我为了使响应对象起作用我必须进行哪些更改?

    最佳答案

    调用RewriteRule时添加的AddRewrite()只是替换了请求中的URI部分(link to source code):

    request.Scheme = scheme;
    request.Host = host;
    request.Path = pathString;
    request.QueryString = query.Add(request.QueryString);
    

    在您的情况下,由于仅替换Path部分,因此ApplyRule方法将非常简单:
    public void ApplyRule(RewriteContext context)
    {
        var request = context.HttpContext.Request;
    
        if (matchPaths.Contains(request.Path.Value))
        {
            request.Path = newPath;
            context.Result = RuleResult.SkipRemainingRules;
        }
    }
    

    关于asp.net-core-2.0 - 自定义网址重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49131720/

    相关文章:

    ASP.Net Core 2.0 - ResponseCaching 中间件 - 不在服务器上缓存

    asp.net-core-2.0 - ConfigurationBuilder 没有定义 AddJson

    asp.net - .NET Core 2 - 创建具有存储库的 Controller 类的实例

    c# - 带有模型绑定(bind)的 Razor 页面 POST 多个表单

    c# - ASP.NET 核心 : Generate Razor Pages for all models

    asp.net-core-mvc - 检索 ASP Net Core 2.0 MVC 中的应用程序版本

    c# - .NET Core Razor 类库似乎无法在 Linux 上运行

    c# - Razor Pages POST 处理程序在 IIS 上返回 404

    c# - 如何动态设置模型而不导致 Razor 页面中的完整回发 (.NetCore 5.1)

    c# - 如何使用 ASP.NET Core 2.0 存储和恢复 cookie?