dotnet / dotnet/aspnetcore

RoutePattern.RawText does not reflect the leading slash in Route attribute

Open
#57,194 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

I have a controller with a route explicitly set with a leading slash using the Route attribute. However, when retrieving the route pattern using HttpContext.GetEndpoint()?.RoutePattern.RawText, the leading slash is omitted.

```csharp
[ApiController]
[Route("/test")]
public class TestController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
var endpoint = HttpContext.GetEndpoint() as RouteEndpoint;
var rawText = endpoint?.RoutePattern.RawText ?? "";
return Ok(new { RoutePattern = rawText });
}
}
```
Result:

```json
{ "routePattern": "test" }
```

### Expected Behavior

The route pattern returned should include the leading slash as explicitly defined in the Route attribute.

```json
{ "routePattern": "/test" }
```

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

8.0.303

### Anything else?

For minimal Apis, it's working as expected.

```csharp
app.MapGet("/minimal", (HttpContext context) =>
{
var endpoint = context.GetEndpoint() as RouteEndpoint;
var rawText = endpoint?.RoutePattern.RawText ?? "";
return Results.Ok(new { RoutePattern = rawText });
});
```
Result:
```json
{ "routePattern": "/minimal" }
```

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.