簡體   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