如何将 .ASPX 页面重定向到 .NET Core Razor 页面

ron*_*san 2 asp.net razor .net-core asp.net-core razor-pages

我们正在将一个大型 asp.net 网站迁移到 .NET Core Razor 页面网站。互联网上到处都有指向我们网站的链接,我们希望这些链接在迁移后能够正常工作。我们将保留相同的 url 格式,但没有扩展名 .aspx。

摘要,我们想要我们的旧网址:

example.com/item.aspx由 .net core razor 页面处理,如下

example.com/item

Zhi*_* Lv 5

您可以使用URL 重写中间件来删除“.aspx”扩展名。

检查以下代码:使用AddRedirect创建重写URL的规则

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        //using Regex match the .aspx extension and remove it.
        var options = new RewriteOptions()
                .AddRedirect(@"(\w*)(.aspx)", "$1");
        app.UseRewriter(options);

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages(); 
        });
    }
Run Code Online (Sandbox Code Playgroud)

然后,截图如下:

在此输入图像描述