Broken encoding after postback

I have a query string with a parameter value that contains the Norwegian character å encoded as %e5 . The page contains a form with an action attribute that is automatically populated by ASP.Net. When the URL is output to the specified attribute, it prints with full double-byte encoding: %u00e5 .

When sending back, this looks fine when debugging code. However, the page actually redirects to itself (for some other reason), and the redirect location header looks like this: Location: /myFolder/MyPage.aspx?Param1=%C3%A5

So, %e5 was transferred to %C3%A5 , which somehow breaks the exit.

In HTML text, broken characters look like å after output via HttpUtility.HtmlEncode.

The entire web application is encoded by ISO8859-1.

PS. When you delete u00 from the output %u00e5 in the action attribute before submitting the form, everything is displayed beautifully. But the error, apparently, is a translation from %e5 to %C3%A5 . (And, of course, a self-regulator, but that's another matter.)

Any pointers?

0
source share
1 answer

The solution I ended up with is manually encoding the redirect URL.

 public void ReloadPage() { UrlBuilder url = new UrlBuilder(Context, Request.Path); foreach (string queryParam in Request.QueryString.AllKeys) { string queryParamValue = Request.QueryString[queryParam]; url.AddQueryItem(queryParam, queryParamValue); } Response.Redirect( url.ToString(), true); } 

In url.AddQueryItem, HttpContext.Server.UrlDecode (queryParamValue) is basically executed, and url.ToString builds a query string, and HttpContext.Server.UrlEncode (queryParamValue) is executed for each query element.

UrlBuilder is a class already in our library, therefore, as soon as I found the problem and realized that C # /. NET does not provide tools for this, the fix coding was fast :)

0
source

Source: https://habr.com/ru/post/1315082/


All Articles