dotnet / dotnet/aspnetcore

AttributeRouteModel Template property does not match @page directives starting with / (slash) or ~ (tilde) (Razor Pages)

Open
#59,931 0 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

Say you have localized your Razor Pages application and created the following convention to prefix all routes with a culture:
```csharp
// Program.cs

builder.Services.AddRazorPages(configure =>
{
configure.Conventions.Add(new CultureConvention());
});

// [...]

public class CultureConvention() : IPageRouteModelConvention
{
public void Apply(PageRouteModel model)
{
var culturePrefix = new AttributeRouteModel(new RouteAttribute("{culture}"));
foreach (var selector in model.Selectors)
{
selector.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(culturePrefix, selector.AttributeRouteModel);
}
}
}
```

The `CombineAttributeRouteModel` method should not combine the two AttributeRouteModels, if the Template property of the right AttributeRouteModel is a "override pattern", i.e., start with a / (slash) or ~ (tilde), but instead keep the right AttributeRouteModel.

So the following page with the following `@page` directive should be unaffected by the convention:
```csharp
@page "~/IgnoreCulture"
@{
ViewData["Title"] = "Ignore Culture";
}

Ignore Culture


```

I.e., it should be accessible from `/IgnoreCulture`.

The Template property of the AttributeRouteModel does not match the `@page` directive, as there is no beginning / (slash) nor ~ (tilde) to be found upon inspecting it, so the two AttributeRouteModel are combined when they shouldn't be.

### Expected Behavior

The result is that the above route is not accessible as expected, but instead have to be accessed using a culture prefix, e.g., `/en-US/IgnoreCulture`.

### Steps To Reproduce

https://github.com/Tobikblom/BugReproduction20250118

### Exceptions (if any)

_No response_

### .NET Version

9.0.200-preview.0.24575.35

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.