繁体   English   中英

Asp.Net核心HTTPContext和RewritePath

[英]Asp.Net Core HTTPContext and RewritePath

我正在将旧的HttpModule转换为新的MiddleWare并想知道在MiddleWare重写路径的正确方法是什么。

旧模块使用此:

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

对于简单的规则,您应该能够通过中间件在context更改request.Path [, SchemeHostQueryString ]。 如下所示,并确保您的中间件在管道中尽早运行:

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

就是说, 不要那样 ,因为ASP.NET Core已经为您想到了所有这些

暂无
暂无

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

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