简体   繁体   English

Asp.Net核心HTTPContext和RewritePath

[英]Asp.Net Core HTTPContext and RewritePath

I am converting over an old HttpModule into a new MiddleWare and want to know what is the correct way for Rewriting the Path in the MiddleWare . 我正在将旧的HttpModule转换为新的MiddleWare并想知道在MiddleWare重写路径的正确方法是什么。

The old module uses this: 旧模块使用此:

context.RewritePath(path, string.Empty, queryPart);

For simple rules, you should be able to just alter the request.Path [, Scheme , Host , QueryString ] in the context on its way through your middleware; 对于简单的规则,您应该能够通过中间件在context更改request.Path [, SchemeHostQueryString ]。 something like below, and ensure your middleware runs early-enough in the pipeline: 如下所示,并确保您的中间件在管道中尽早运行:

internal class PathRewritingMiddleware
{
    private readonly RequestDelegate _next;

    public PathRewritingMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public Task Invoke(HttpContext context)
    {
        context.Request.Path = "elsewhere/" + context.Request.Path;
        return _next(context);
    }
}

That said, don't , as ASP.NET Core has already thought of all of this for you. 就是说, 不要那样 ,因为ASP.NET Core已经为您想到了所有这些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM