在Kestrel中重写后获取原始URL

时间:2017-07-17 19:58:52

标签: asp.net-core kestrel

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

Kestrel将重写的URL传递给管道(可通过Error in fromJSON(content, handler, default.size, depth, allowComments, : Invalid JSON Node 访问)。

可以在重写后的中间件中访问原始网址吗?

1 个答案:

答案 0 :(得分: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"] });
}