Jonathan Wood
Jonathan Wood

Reputation: 67193

Using routes to change page name and extension

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>

Upvotes: 1

Views: 991

Answers (1)

Mike Brind
Mike Brind

Reputation: 30045

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

Upvotes: 2

Related Questions