1

I am using blazor wasm, I want to create a custom routing that starts with the letter @ and ends with the User's username. I wrote the following code, but it seems that the routing cannot be set this way

the route I expect https://localhost:5000/@michel

@page "/@{Username}"

@code {

[Parameter]
public string Username { get; set; }

}
jack
  • 71
  • 9

1 Answers1

0

You can't segment routes with ascii characters. "@" is just an ascii character. So:

https://localhost:5000/@michel

looks for a route "@michel", and will only find one if you have this on a page:

@page "/@michel"

Put in a separator like this:

@page "/@/{UserName}"

and that will work.

If you want to route using "@" as a special character (like "/") then I believe you're into building a custom router.

MrC aka Shaun Curtis
  • 19,075
  • 3
  • 13
  • 31
  • There is no problem using the @ character in url https://stackoverflow.com/questions/19509028/can-i-use-an-at-symbol-inside-urls I want to know how I can route a default character first, not a segment like `/@/michel` – jack Jul 25 '22 at 17:38
  • I didn't say there was a problem with @. I'm just pretty sure you can't use it as a route separator in the standard router - happy to be wrong and someone show us how. So I think the answer to "I want to know how I can route a default character first" is write yourself a router. – MrC aka Shaun Curtis Jul 25 '22 at 17:53
  • If I save the username in the database as ‍`@michel`, how can I be sure that if the user starts the address with @, the component I want will receive the request, because it is possible that other routes match this address. – jack Jul 25 '22 at 18:26
  • ??? [short for polite question] Are you saying "https://localhost:5000/@michel" and "https://localhost:5000/michel" should both route to the same place and `UserName` should capture "michel"? – MrC aka Shaun Curtis Jul 25 '22 at 18:35
  • No, I want to save the username with @ in the database and if the user enters this address ‍`localhost:5000/@michel` The request must reach the component I want because it is possible that other routes will match and the wrong component will be loaded. – jack Jul 25 '22 at 18:53
  • If you want to save the username with @ in database, I think you don't need to let user enters UserName starting with `@` in URL, you can `UserName = "@"+UserName;` in `OnInitialized()` method. – Xinran Shen Jul 26 '22 at 06:57
  • @jack - see my answer to your other (very similar) question. – MrC aka Shaun Curtis Jul 26 '22 at 11:37