How to redirect .ASPX pages to .NET Core Razor page

ぃ、小莉子 提交于 2021-02-11 14:57:32

问题


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


回答1:


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:



来源:https://stackoverflow.com/questions/64756945/how-to-redirect-aspx-pages-to-net-core-razor-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!