HttpUtility.HtmlEncode after postback asp.net - asp.net

I have this in the url:
http://localhost:5466/Supplier.aspx?id=Orchidée organisation
After a postback on the page the url changes to:
http://localhost:5466/Supplier.aspx?id=Orchid%C3%A9e%20organisation
How can i maintain the first url after postback

How can i maintain the first url after post back
You don't. The first URL is technically invalid because it the value isn't URL-encoded. Most browsers will silently correct this for you. But the actual URL is:
http://localhost:5466/Supplier.aspx?id=Orchid%C3%A9e%20organisation
Because the URL-encoded version of:
Orchidée organisation
is:
Orchid%C3%A9e%20organisation
(Note: This has nothing to do with HTML-encoding, as your question title suggests.)
If you're receiving URL-encoded values somewhere in your server-side code, HttpServerUtility has a UrlDecode() function which can decode them for you.

Related

Navigating to decoded URL doesn't elicit the same action as navigating to the encoded URL

Trying to understand why pasting the first link works but not the second one.
Breakdown of the URL, for a clearer view:
Encoded version: [works]
http%3A%2F%2FsomeSite.com
%2FDownload.ashx
%3Frequest
%3DIL7zxW6ETqiYU6cThSNKL8MpY
%252bCRIVFZAVhd8DYPG85C1Uhdd
%252f2hqqmoObeNmuS3dg4bDgGBb0kUUxGZhej89kTaLBHBXS
%252bq3tlaEk2uMEcbWlUZzZQs00sirwZ2IvAvoSpU7HC3N1FaYSNciQ4iHNNmTU
%252f6uMypNlPOJ6enlbZ1OrrYODkaMRdRfGKEba
%252brusdryM4gp
%252bopi1a0gNuMQVCtj
%252bAvDcgXGOcZPNhPAnE
%253d&version=Ma88r6Z6t2JQcnVhVXgp0A%3D%3D
Decoded version: [doesn't work]
http://someSite.com
/Download.ashx
?request=
IL7zxW6ETqiYU6cThSNKL8MpY
+CRIVFZAVhd8DYPG85C1Uhdd
/2hqqmoObeNmuS3dg4bDgGBb0kUUxGZhej89kTaLBHBXS
+q3tlaEk2uMEcbWlUZzZQs00sirwZ2IvAvoSpU7HC3N1FaYSNciQ4iHNNmTU
/6uMypNlPOJ6enlbZ1OrrYODkaMRdRfGKEba
+rusdryM4gp
+opi1a0gNuMQVCtj
+AvDcgXGOcZPNhPAnE
=&version=Ma88r6Z6t2JQcnVhVXgp0A==
If I paste the first link in the browser - it works. A file download automatically starts.
If I paste the second link in the browser - page says Bad request.
Can anyone clarify it for me why the second one doesn't work?
Quoting the URLencodetag:
To “URL encode” or “percent encode” text means to encode it for use in a URL. Some characters are not valid when used as-is in URLs, and so much be URL-encoded (percent-encoded) when appearing in URLs.
The encoding was used for a reason, here because the base64 values for the request and version parameters contains +, / and = which have their own meaning in URLs and therefore need to be URL-encoded.

Can anyone help me to redirect to the selected country in this autocomplete form on submit?

Can anyone help me in the right direction? I am still quite new to coding.
I found this script on W3 school that I would like to adjust to my needs a little.
On submit I want to be redirected to : www.url.com/?mycountry=selectedcountry.
Is this possible and how to get there? Thanks a lot Martin
https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_autocomplete
You may add method="GET" to your <form> here is the example https://www.w3schools.com/tags/att_form_method.asp
That will help you to do the thing
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).
The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").
Notes on GET:
Appends form-data into the URL in name/value pairs
The length of a URL is limited (about 3000 characters)
Never use GET to send sensitive data! (will be visible in the URL)
Useful for form submissions where a user wants to bookmark the result
GET is better for non-secure data, like query strings in Google
Notes on POST:
Appends form-data inside the body of the HTTP request (data is not
shown in URL)
Has no size limitations
Form submissions with POST cannot be bookmarked

Querystring in C# web application

I have created web application.I am giving one of web page to client as api.Client can pass parameter to web page like below
Ex: www.domainname.com/Testpage.aspx?name=pinky&city=pune&number=xxxxxxxx
In same page Testpage.aspx,I am accessing/fetching querystring like below.
string s= Request.Querystring["name"];
I am not sure how client can call api.I mean to say from browser or code throught.Whether client use urlencode or not?
from code
www.domainname.com/Testpage.aspx?name=Server.UrlEncode("pinky")&city=Server.UrlEncode("pune")&number=Server.UrlEncode("xxxxxxxx")
will below code work ? or does i need to decode?If client did not use Encode then decode work fine?.I want user querystring value further processing and insert into table.
string s= Request.Querystring["name"];
You need not decode it. If they have entered special characters and not encoded then it will not reach your server-side code at all because it will throw a bad request error. If they have encoded at their end then it will be automatically decoded at your end.
Even If they have not encoded, Your decode will work fine.

ASP.NET Form Action Invalid Percent Encoding

I have a web application that places the user's search term in the query string, in a similar way to Google. E.g. the address might be www.example.com/mysearchpage.aspx?q=searchTerm.
Usually this works fine, but if there is a special character in the search term such as â, the action attribute on the form is encoded to percent encoding and the character is replaced with %u00e2.
If I search for chât I will end up with the URL www.example.com/mysearchpage.aspx?q=châtin the browser's address bar but the action attribute on the form that comes back from the server would be www.example.com/mysearchpage.aspx?q=ch%u00e2t which means that a subsequent form submission fails because the URL is incorrectly formatted.
I have ensured that in IIS the encoding is set to be UTF-8 for Requests, Response Headers and Responses. I have also inspected the page being delivered from IIS in Fiddler and that already includes the incorrectly encoded action.
The encoded format appears to be in a non-standard format as explained in this wikipedia article.
Is there a way to prevent IIS from encoding the form's action in this way?
The solution was to add targetFramework=4.5.2 into the httpRuntime tag in the web.config file.
Previously this was not specified but was specified in the compilation tag, however specifying targetFramework=4.5.1 still caused the problem.

In ASP.NET, how do I modify POST data and then redirect/POST it to another URL?

I have a page that's sending some POST data in plaintext to an aspx page I wrote:
firstname: John
lastname: Doe
accountid: 123
I need to take this POST data and run it through an encrypting algorithm so the data ends up like so:
firstname: AKFJULKHI
lastname: IDKLNZUI
accountid: RIQLKKNIC
After encoding it, I then need to POST it to another aspx page that's expecting this encoded data. I cannot modify this page. Essentially, I'm writing an in-between page whose purpose is to take some plaintext data, encode it using a proprietary algorithm, and pass it to another page that expects this encrypted data. I realize it's a useless security feature because the user knows what the data is before the encryption, but I cannot modify the other pages and have to stick to the API.
My question is, what is the easiest way of doing this? Response.Redirect will convert it to a GET request, and Server.Transfer will not change the URL on the client side. I could do a manual POST request and then do a Response.Write, but that seems like a really roundabout way of doing it.
I think you may want to approach this with a module instead. The HttpModule intercepts the request, can change the request and then the request continues on to the target and the target page is unaware that the request was modified.,
ref: http://www.codeproject.com/Articles/30907/The-Two-Interceptors-HttpModule-and-HttpHandlers#HttpModuletheeventbasedpreprocessor
HTTP does not support POST redirects.
Instead, you can render a simple page with an HTML form containing hidden inputs with your modified data and pointing to the new URL, then automatically submit the form using Javascript.

Resources