【问题标题】:Get original URL after rewriting in Kestrel在 Kestrel 中重写后获取原始 URL
【发布时间】:2017-07-17 19:58:52
【问题描述】:

Apache 会根据重写的 URL 选择要提供的文件,但原始 URL 会被传递给脚本。

Kestrel 将重写后的 URL 向下传递(可通过 HttpContext.Request.Path 访问)。

是否可以在中间件重写后访问原始 URL

【问题讨论】:

  • 编写一个中间件,读取它并将其传递到管道中(通过HttpContext.Items 或任何其他适合您的方式)

标签: asp.net-core kestrel


【解决方案1】:

按照@Tseng 发出的指示。我的测试包装了 RewriteMiddleware,但您可能需要一个单独的中间件。

public class P7RewriteMiddleware
{
    private RewriteMiddleware _originalRewriteMiddleware;

    public P7RewriteMiddleware(
        RequestDelegate next,
        IHostingEnvironment hostingEnvironment,
        ILoggerFactory loggerFactory,
        RewriteOptions options)
    {
        _originalRewriteMiddleware = new RewriteMiddleware(next, hostingEnvironment, loggerFactory, options);
    }

    /// <summary>
    /// Executes the middleware.
    /// </summary>
    /// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
    /// <returns>A task that represents the execution of this middleware.</returns>
    public new Task Invoke(HttpContext context)
    {
        var currentUrl = context.Request.Path + context.Request.QueryString;
        context.Items.Add("original-path", currentUrl);
        return _originalRewriteMiddleware.Invoke(context);
    }
}

稍后,我的身份验证过滤器使用它。

if (spa.RequireAuth)
{
   context.Result = new RedirectToActionResult(Action, Controller,
         new { area = Area, returnUrl = context.HttpContext.Items["original-path"] });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-26
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 2011-04-10
    相关资源
    最近更新 更多