RoutePattern.RawText does not reflect the leading slash in Route attribute
- 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
Assessment
This issue has not been assessed yet.