代码之家  ›  专栏  ›  技术社区  ›  Maritim

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

  •  2
  • Maritim  · 技术社区  · 7 年前

    我正在尝试将.NETCore中的一个URL重定向到另一个URL。原始URL为

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

    下面是my 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 回复  |  直到 7 年前
        1
  •  1
  •   Maritim    7 年前

    事实证明,我有一个拦截网站。配置规则,所以这个故事的寓意是完全移植您的web。配置到.NET核心规则,而不仅仅是部分:)要彻底!