1

I'm porting an old WebForms application to .NET Core Razor Pages.

I have a few cases where I'd like to retain the old URLs. I have a Razor Page called Resources/CharClass, but I'd like the URL to be Resources/CharClass.aspx.

I tried the following, but it doesn't route to the desired page.

@page "{title=CharClass.aspx}"
@model TestRazorPages.Pages.Resources.CharClassModel
@{
    ViewData["Title"] = "CharClass";
}

<h1>CharClass</h1>

1 Answer 1

2

What you have done is to add a route data parameter placeholder and given it a default value, rather than create a new route (https://www.learnrazorpages.com/razor-pages/routing#route-templates).

In your ConfigureServices method in StartUp, add the following in Razor Pages 2.2:

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.AddPageRoute("/Resources/CharClass", "/Resources/Charclass.aspx");
});

If you are using .NET 3.0, chain the call to AddRazorPagesOptions to services.AddRazorPages()

More about the additive routes here: https://www.learnrazorpages.com/razor-pages/routing#friendly-routes

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I'm new to Razor Pages. Do you know if there's a way to instead do a 301 redirect without creating a separate page for the redirect?
You could use middleware for that: learnrazorpages.com/middleware. In the Invoke method, check Request.Path and redirect accordingly, setting the HTTP status accordingly and the location header.
Is there a way to remove the old route? I struggled with that here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.