Rewrite URL in ASP.Net Core Razor Pages (not MVC)

Viewed 643

By seo friendly, I am trying to use the rewrite middleware to rewrite an url to a seo friendly url, but I am not successful.

As an example, what I want to do is rewrite the url https://example.com/1 to https://example.com/test-1 and the url https://example.com/1/2 to https://example.com/test-1/test-2.

I leave the Startup and ChangeURL classes that I have made.

Startup.cs:

    public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        var options = new RewriteOptions();
        options.Rules.Add(new ChangeUrl());
        app.UseRewriter(options);

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

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

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
    }
}

ChangeURL.cs:

    public class ChangeUrl : IRule
{
    public void ApplyRule(RewriteContext context)
    {
        var request = context.HttpContext.Request;

        Match m1 = Regex.Match(request.Path.Value, @"^/(\d+)");
        Match m2 = Regex.Match(request.Path.Value, @"^/(\d+)/(\d+)");

        if (m1.Success)
        {
            request.Path = "/test-1";
        }
        else if (m2.Success)
        {
            request.Path = "/test-1/test-2";
        }
        context.Result = RuleResult.ContinueRules;
        return;
    }
}

I appreciate all the help you can give me.

1 Answers

As far as I know, asp.net core contains the url rewrite module which is used to url rewrite it.

More details, you could refer to below codes:

         // Add below codes into the Configure method
        var options = new RewriteOptions()
.AddRewrite(@"^(\d+)/(\d+)", "test-$1/test-$2",
    skipRemainingRules: true)
.AddRewrite(@"^(\d+)", "test-$1",
    skipRemainingRules: true);

        app.UseRewriter(options);

More details, you could refer to this artcle.

       public void ApplyRule(RewriteContext context)
        {
            var request = context.HttpContext.Request;

            Match m1 = Regex.Match(request.Path.Value, @"^/(\d+)");
            Match m2 = Regex.Match(request.Path.Value, @"^/(\d+)/(\d+)");

            if (m1.Success)
            {
                request.Path = "/home/Privacy";
            }

            if (m2.Success)
            {
                request.Path = "/home/account";
            }
           
                context.Result = RuleResult.ContinueRules;

            
            return;
        }
Related