2

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

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

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

4

1 回答 1

0

遵循@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"] });
}
于 2017-09-16T18:03:42.153 回答