Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor routing in .NET 8 interpreting %2f as / #53668

Closed
danroth27 opened this issue Jan 27, 2024 Discussed in #53622 · 4 comments
Closed

Blazor routing in .NET 8 interpreting %2f as / #53668

danroth27 opened this issue Jan 27, 2024 Discussed in #53622 · 4 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@danroth27
Copy link
Member

Discussed in #53622

Originally posted by gmeister99 January 25, 2024
Hi all, I realise using an encoded forward slash (%2f) in a URL is not advised, however we have a website that uses the 'code' for a record in the URL. A code could be ABC/123. We then use it in the URL to determine what data to display on a generic page, e.g. /somepage/ABC%2f123

This used to work fine, as in the routing would go to the Blazor page with the @page directive "/somepage/{code}. But now, after upgrading the project to .NET 8, this url - /somepage/ABC%2f123 - experiences a page not found error, as it is interpreted by the Blazor routing engine as /somepage/ABC/123, which would be looking for a Blazor page with the @page directive matching "/somepage/{param1}/{param2}", which doesn't exist.

The robust solution would be to change the page to not use the 'code' in the URL, to completely avoid forward slashes. However, this page has worked for a couple of years like this without issue and is now impacting production, so I'm looking for a quick fix. Does anyone know of a setting, or some config that was changed/introduced somwhere in .NET8 that would influence routing?

Thanks

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Jan 27, 2024
@gmeister99
Copy link

Thanks @danroth27, we ended up resolving this the quick way by moving the offending param into the query string, which doesn't impact Blazor routing. Our research of %2f in URLs indicates that it's a long standing behaviour, with the key takeaway being - don't include %2f in a URL. It's just odd that this used to work for us, but we decided to be on the safe side and make the change from our end.

@gragra33
Copy link

gragra33 commented Jan 28, 2024

Discussed in #53622

Does anyone know of a setting, or some config that was changed/introduced somwhere in .NET8 that would influence routing?

Thanks

@danroth27 & @gmeister99 A quick fix could be to use base64 strings. The code would remain unchanged, just an extra step to encode and decode. This allows for other illegal characters, if required.

Update: The other workaround could be to over encode:

string code = "abc/123";

string encoded = WebUtility.UrlEncode(code);
Console.WriteLine(encoded);

string urlEncoded = WebUtility.UrlEncode(encoded);
Console.WriteLine(urlEncoded);

string urlDecoded = WebUtility.UrlDecode(urlEncoded);
Console.WriteLine(urlDecoded);

string decoded = WebUtility.UrlDecode(urlDecoded);
Console.WriteLine(decoded);

Which outputs the following:

abc%2F123
abc%252F123
abc%2F123
abc/123

@javiercn
Copy link
Member

This was already fixed in main, and have a patch candidate #53538

@javiercn javiercn added question ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. labels Jan 29, 2024
@ghost ghost added the Status: Resolved label Jan 29, 2024
@ghost
Copy link

ghost commented Jan 30, 2024

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

4 participants