How to encode back an escaped url?

Viewed 24

I have the following code tied to each item of a dropdown-menu:

var url = Url.ActionContext.HttpContext.Request.GetDisplayUrl();

var uri = new Uri(url).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
var cultureEscaped = Uri.EscapeDataString(item.Name);
var uriEscaped = Uri.EscapeDataString(uri);

url = $"/Culture/Set?culture={cultureEscaped}&redirectUri={uriEscaped}";

The original url that reaches this page might be:

https://myapp.com/Login?ReturnUrl=%2Fconnect%2Fauthorize%3Fclient_id%3Dclientid%26redirect_uri (etc)

The above code is changing the url to:

https://myapp.com/Login?ReturnUrl=/connect/authorize?client_id=clientid&redirect_uri (etc)

I do not want this. I need to have the ReturnUrl parameter encoded back to match the original string.

How can I do this?

0 Answers
Related