question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remove magic string for razor component @page routing

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

In the Blazor and Razor Pages components we are specify the route like:

@page "/this/is/my/route"

These magic string are not desirable in a modern programming context. And also it become difficult to manage because of duplication - good probability you must write it somewhere else also, and now you have two magic strings… for every route.

Describe the solution you’d like

Please can we have no magic string for something so important like routing?

Additional context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
denisz1commented, Sep 30, 2022

Something like that is good

public static class Routes
{
  public const string Route1 = "/Hello";
  public const string Route2 = "/World/Hi";
}
@page Routes.Hello

There many ways to do this, anything you choose is better to magic strings.

1reaction
SSchulze1989commented, Sep 30, 2022

Not the author but have come across this issue a few times. Especially updating all nav-links to the pages when a part of the route changes is my biggest issue i have here.

Ideally you could just use any const string that is known at compile time. For example using a static helper class holding the strings:

public static class RouteConstants 
{
    public const string This = "this";
    public const string Is = "is";
    public const string My = "my";
    public const string Route = "route";
    public const string ThisIsMyRoute = $"/{This}/{Is}/{My}/{Route}"
}

and then use string interpolation in the actual route

@page $"{RouteConstants.This}/{RouteConstants.Is}/{RouteConstants.My}/{RouteConstants.Route}"

Or you use the constant for the complete route

@page RouteConstants.ThisIsMyRoute

Then you could easily link to these pages

<a href=@RouteConstants.ThisIsMyRoute>Link</a>

And not worry about changing your Route.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Feature Request] Remove magic strings from RazorPages ...
A RazorPages route can be defined like this: @page "/Foo" But ... [Feature Request] Remove magic strings from RazorPages routing #41930.
Read more >
Avoid magic string in ASP.NET Core attribute routing
I can avoid magic strings for areas, controllers, actions by using the special tokens [controller] , [action] , etc. This is a simple...
Read more >
Localising the DisplayAttribute and avoiding magic strings ...
In this post, we cover some of the problems you may find when localising your application such as localising the DisplayAttribute and broken ......
Read more >
ASP.NET Core Blazor routing and navigation
This article explains how to manage Blazor app request routing and how to use the NavLink component to create navigation links.
Read more >
Blazor Power Hour: Razor Class libraries and Shared Routing
Today on the #Blazor Power Hour. We'll look at Razor Class libraries and routing from dependent assemblies. The stream starts at 02:27, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found