Get original URL after rewriting in Kestrel

我是研究僧i 提交于 2019-12-11 07:32:36

问题


Apache would choose a file to serve based on rewritten URL, but the original URL would be passed to the script.

Kestrel passes the rewritten URL down the pipeline (accessible via HttpContext.Request.Path).

Is it possible to access original URL from Middleware after it has been rewritten?


回答1:


Following the direction issued by @Tseng. My test wraps the RewriteMiddleware, but you may want a separate middleware.

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);
    }
}

Later, my auth filter uses it.

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


来源:https://stackoverflow.com/questions/45152655/get-original-url-after-rewriting-in-kestrel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!