简体   繁体   English

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

[英]How to redirect .ASPX pages to .NET Core Razor page

We are moving a big asp.net web site to .NET Core Razor pages site.我们正在将一个大型 asp.net 网站迁移到 .NET Core Razor 页面站点。 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.我们将保留相同的 url 格式,但没有扩展名 .aspx。

Summary, we want our old url:总结,我们想要我们的旧网址:

example.com/item.aspx be handle by .net core razor page, as example.com/item.aspx由 .net core razor 页面处理,如

example.com/item例子.com/item

You could use the URL Rewriting Middleware to remove the ".aspx" extensions.您可以使用URL 重写中间件删除“.aspx”扩展名。

Check the following code: Use AddRedirect to create a rule for rewriting URLs检查以下代码:使用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(); 
        });
    }

Then, the screenshot like this:然后,截图是这样的:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何有条件地重定向到基于 ASP.NET Core Razor 页面中的单选按钮选择的页面? - How to conditionally redirect to a page based on Radio Button Selection in ASP.NET Core Razor Pages? 如何在 ASP.Net Core Razor 页面上重定向 - How to redirect on ASP.Net Core Razor Pages 如何在 ASP.NET Core 中将控制器重定向到剃刀页面 - How to Redirect Controller to razor page in ASP.NET Core 重建 My.ASPX 网站,但如何轻松重定向到 .NET 核心页面 [在共享主机上]? - Rebuilt My .ASPX Website But How to Easily Redirect to .NET Core Pages [on Shared Hosting]? 如何重定向到 razor 页面中的自定义 404 页面 - How to redirect to custom 404 page in razor pages 在ASP.NET Core Razor Pages中,如何获取该页面上下文之外的页面的视图引擎路径? - In ASP.NET Core Razor Pages, how to get view engine path for a page outside that page's context? 在 ASP.NET Core Razor 页面上提交 Post 后如何刷新页面内容? - How to refresh the page content after a Post submission on ASP.NET Core Razor Pages? 如何提交 asp.net 核心 razor 页面表单而不重新加载页面 - How to submit asp.net core razor pages form without reloading page 如何在 ASP.NET Core Razor Pages 项目的 _layout.cshtml 文件中使用 Razor 页面使用实体框架作为部分视图? - How to use Razor Page Using Entity Framework as a partial view in _layout.cshtml file in ASP.NET Core Razor Pages project? 如何通过 Razor 页面扩展 ASP.NET 核心 MVC 项目? - How to extend an ASP.NET Core MVC project by Razor Pages?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM