Hieu Le
Hieu Le

Reputation: 1132

Cannot use Vietnamese / signed & space characters in URL parameters in Blazor

I use blazor with .net core 3.0 to develop a website that allow to pass some parameters in URL.

The problem is whenever I pass a Vietnamese keyword in the URL, the blazor throw an inner exception that appears on Browser console.

Please be aware of that I cannot use Encode URL to extract that information since the blazor throw exceptions by it-self before OnAfterRenderAsync calling

Work

https://localhost:44316/?keyword=tieng viet
https://localhost:44316/?keyword=tieng%20viet

Not Work

https://localhost:44316/?keyword=tiếng việt
https://localhost:44316/?keyword=tiếng%20việt
https://localhost:44316/?keyword=ti%E1%BA%BFng%20vi%E1%BB%87t

How to reproduce:

=> Since I don't add any extra code here, the blazor should not throw any exceptions

Here is the exception: Here is the exception

Thanks for help!

Upvotes: 2

Views: 1087

Answers (2)

itminus
itminus

Reputation: 25350

I have to encode and decode every time

Not sure whether it is a bug. However, you don't have to encode and decode every time. As a walkaround, we can create a quick and dirty fix so that the space within the querystring is converted to +.

Since this error happens when invoking remote signalR ComponentHub::StartCircuit() method, we can replace the location before it is passed to siganlR. Based on @mjwills's above comment that suggests localhost:44316/?keyword=ti%E1%BA%BFng+vi%E1%BB%87t, you can add a script in your _Host.cshtml as below:

<script>
    !function(){
        var raw = new URL(window.location.href);
        raw.search = raw.search.replace("%20","+");    // replace the `%20` with "+"
        window.history.replaceState('', document.title, raw);
    }();
</script>
<script src="_framework/blazor.server.js"></script>

By this way, your server side code doesn't have to care about the encoding.

Upvotes: 2

Postlagerkarte
Postlagerkarte

Reputation: 7117

First things first: This is nothing Blazor specific

You are simply using an URI which is not valid. Each character used in an URI must have a corresponding characters via US-ASCII table.

Blazor is just calling Uri.IsWellFormedUriString which returns false for your given example.

As others have pointed out the solution is to encode the url. This has to be done before that URL is used to navigate to a blazor page.

Upvotes: 1

Related Questions