我正在将旧的HttpModule
转换为新的MiddleWare
,并想知道在MiddleWare
中重写路径的正确方法是什么。
旧模块使用这个:
context.RewritePath(path, string.Empty, queryPart);
对于简单的规则,您应该能够在context
中通过中间件更改request.Path
[,Scheme
,Host
,QueryString
];如下所示,并确保您的中间件在管道中足够早地运行:
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已经为您考虑了所有这些。