如何将 .ASPX 页面重定向到 .NET Core Razor 页面 [英] How to redirect .ASPX pages to .NET Core Razor page

查看:46
本文介绍了如何将 .ASPX 页面重定向到 .NET Core Razor 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

总结,我们想要我们的旧网址:

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

example.com/item

解决方案

您可以使用

We are moving a big asp.net web site to .NET Core Razor pages site. All over the internet there are links that point to our site and we want those links to work after our migration. We will kept the same url format, but without the extension .aspx.

Summary, we want our old url:

example.com/item.aspx be handle by .net core razor page, as

example.com/item

解决方案

You could use the URL Rewriting Middleware to remove the ".aspx" extensions.

Check the following code: Use AddRedirect to create a rule for rewriting URLs

    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(); 
        });
    }

Then, the screenshot like this:

这篇关于如何将 .ASPX 页面重定向到 .NET Core Razor 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆