Microsoft.AspNetCore.Rewrite中的URL重定向中间件不会重定向

时间:2017-06-28 08:18:03

标签: regex asp.net-core url-redirection

我正在尝试将一个URL重定向到.NET Core中的另一个URL。原始网址是

http://localhost:3000/en/ad/test/1758

及以下,您会看到我的Startup.cs的Configure()方法。出于某种原因,这个规则永远不会被我看到。如果我将正则表达式更改为^(.*)$,它当然会每次都会出现,但是正如您在下面看到的正则表达式那样,它无法正常工作。

为什么?

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory)
{
    app.UseRewriter(new RewriteOptions().AddRedirect(@"^en/ad/.*/(\d+)$", this.Configuration["Api:PublicUrl"] + "/en/ad/$1"));

    applicationLifetime.ApplicationStopping.Register(OnShutdown);

    loggerFactory.AddConsole();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    // Serve wwwroot as root
    app.UseFileServer();

    // Serve /node_modules as a separate root (for packages that use other npm modules client side)
    app.UseFileServer(new FileServerOptions()
    {
        // Set root of file server (remember not wwwroot!)
        FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "node_modules")),
        // Only react to requests that match this path
        RequestPath = "/node_modules"
    });
}

1 个答案:

答案 0 :(得分:1)

事实证明我有一个拦截web.config规则,因此故事的寓意是将web.config完全移植到.NET Core规则而不仅仅是部分:)彻底!!/ / p>