在Kestrel中重写后获取原始URL [英] Get original URL after rewriting in Kestrel
本文介绍了在Kestrel中重写后获取原始URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Apache会根据重写的URL选择要提供的文件,但是原始URL将传递给脚本.
Apache would choose a file to serve based on rewritten URL, but the original URL would be passed to the script.
Kestrel将重写的URL传递到管道(可通过HttpContext.Request.Path
访问).
Kestrel passes the rewritten URL down the pipeline (accessible via HttpContext.Request.Path
).
在重写后是否可以从中间件访问原始URL ?
Is it possible to access original URL from Middleware after it has been rewritten?
推荐答案
按照@Tseng发布的说明进行操作.我的测试包装了RewriteMiddleware,但是您可能需要单独的中间件.
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"] });
}
这篇关于在Kestrel中重写后获取原始URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文