ASP.NET URL Encoding/Decoding - asp.net

I have two files htmlpage1.htm and webform1.aspx
htmlpage1.htm contains a anchor tag with href="webform1.aspx?name=abc+xyz".
When i try to access the querystring in page_load of webform1.aspx, i get "abc xyz" (abc [space] xyz). I want exact value in querystring "abc+xyz"
Note: href value cannot be changed
Any help will be appreciated
Thank You.

This will Server.UrlDecode for you:
Request.QueryString["name"] // "abc xyz"
Option 1) You can re-encode
Server.UrlEncode(Request.QueryString["name"]); // "abc+xyz"
or get the raw query data
Request.Url.Query // "?name=abc+xyz"
Option 2) Then parse the value
Request.Url.Query.Substring(Request.Url.Query.IndexOf("name=") + 5) // "abc+xyz"

ASP.net will decode the query string for your. you can get the raw query string and parse it yourself if you want.

Try webform1.aspx?name=abc%2Bxyz

Use this :
Request.QueryString["name"].Replace(" ","+");
// Refer below link for more info
http://runtingsproper.blogspot.in/2009/10/why-aspnet-accidentally-corrupts-your.html

Related

ASP.NET if root URL contains query string, pass it to default.aspx on redirect

Is there any way that I can preserve a query string and pass it to the default.aspx;
For example:
http://www.example.com/?test=123
becomes
http://www.example.com/Default.aspx?test=123
Thanks in advance!
Yes you can. You could use Request.QueryString["test"] to get the value from query string and pass it to the other page using
if(Request.QueryString["test"] != null)
{
Response.Redirect("~/Default.aspx?test=" + Request.QueryString["test"].ToString())
}
Do this in Session_Start method of Global.asax.cs
Or if you want to pass the entire query string instead of one value just use Request.QueryString.ToString()

Accessing the query string value using ASP.NET

I have been trying to find the question to my answer but I'm unable to and finally I'm here. What I want to do is access the value passed to a webpage (GET, POST request) using asp.net. To be more clear, for example:
URL: http://www.foobar.com/SaleVoucher.aspx?sr=34
Using asp.net I want to get the sr value i.e 34.
I'm from the background of C# and new to ASP.NET and don't know much about ASP.NET.
Thanx.
Can you refer to this QueryString
Here he says how to access the query string using:
Request.Url.Query
That is not called a Header, but the Query String.
the object document.location.search will contain that and the javascript to get any query string value based on the key would be something like:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
code from other question: https://stackoverflow.com/a/901144/28004

asp .net query string encoding and decoding

I type the following url into my web browser and press enter.
http://localhost/website.aspx?paymentID=6++7d6CZRKY%3D&language=English
Now in my code when I do HttpContext.Current.Request.QueryString["paymentID"],
I get 6 7d6CZRKY=
but when I do HttpContext.Current.Request.QueryString.ToString() I see the following:
paymentID=6++7d6CZRKY%3D&language=English
The thing I want to extract the actual payment id that the user typed in the web browser URL. I am not worried as to whether the url is encoded or not. Because I know there is a weird thing going on here %3D and + sign at the same time ! But I do need the actual + sign. Somehow it gets decoded to space when I do HttpContext.Current.Request.QueryString["paymentID"].
I just want to extract the actual payment ID that the user typed. What's the best way to do it?
Thank you.
You'll need to encode the URL first, using URLEncode(). + in URL equals a space so needs to be encoded to %2b.
string paymentId = Server.UrlEncode("6++7d6CZRKY=");
// paymentId = 6%2b%2b7d6CZRKY%3d
And now
string result = Request.QueryString["paymentId"].ToString();
//result = 6++7d6CZRKY=
However
string paymentId = Server.UrlEncode("6 7d6CZRKY=");
//paymentId looks like you want it, but the + is a space -- 6++7d6CZRKY%3d
string result = Request.QueryString["paymentId"].ToString();
//result = 6 7d6CZRKY=
There is some info on this here: Plus sign in query string.
But I suppose you could also use a regular expression to get your parameter out of the query string. Something like this:
string queryString = HttpContext.Current.Request.QueryString.ToString();
string paramPaymentID = Regex.Match(queryString, "paymentID=([^&]+)").Groups[1].Value;
I sent an Arabic text in my query string
and when I resieved this string it was Encoded
after Server.UrlDecode
departmentName = Server.UrlDecode(departmentName);
it back again to arabic
I hope this help you

How do I get the un-decoded request URL under ASP.NET?

My request URL is: http://domain.com/some/path%2Fescaped.
I want to retrieve this exact URL. I do NOT want it decoded, like http://domain.com/some/path/escaped; I want it encoded like http://domain.com/some/path%2Fescaped.
How do I get this URL? I have tried Request.Path, Request.RawUrl, Request.Url.AbsoluteUri, Request.Url.OriginalString...each provide the URL decoded, like http://domain.com/some/path/escaped.
I can get this in PHP with $_SERVER["REQUEST_URI"].
I don't know if this is going to work or not, but have you already tried Server.URLDecode or Server.URLEncode?
Javascript call document.location.href should return the expected format. Here is a suggestion; Check if this works for you.
1) have a hidden variable
<input type="hidden" id="hdn" runat="server" />
2) Set hidden variable using javascript function
function setURL() {
document.getElementById("hdn").value = document.location.href;
}
3) on server side
Page.ClientScript.RegisterStartupScript(this.GetType(),
"setURL", "setURL();", true);
4) read hidden variable value at server side (which is the URL in actual format)
hdn.Value
You need Request.Url.OriginalString;

Asp.net pass value from txtbox to an another page

This the end of my code
...
If lblErrMsg.Text = "" Then
Response.Redirect("UserPage.aspx")
End If
I want to pass the value of txtUser(I create It in the current page...) to the UserPage.aspx.
Thank's for helping me ...
This is in VB.net not in c# Please
C# Version
1) Use querystring
Response.Redirect("user.aspx?val="+txtBox.Text);
and in userp.aspx.cs,
string strVal=Request.QueryString["val"];
2)Use Session
Setting session in first page before redirecting
Session["val]=txtBox.Text;
Response.Redirect("user.aspx");
and in user.aspx.cs
String strVal=(string) Session["val"];
EDIT :VB.NET VERSION
1) Use Querystring
Response.Redirect("user.aspx?val=" + txtBox.Text)
and in user.aspx.vb
Dim strVal As String = Request.QueryString("val")
2)Use Session
Setting Session in firstpage
Session("val")=txtBox.Text
Response.Redirect("user.aspx")
and in user.aspx.vb.
Dim strVal As String = DirectCast(Session("val"), String)
You can pass it in the query string, like this:
Response.Redirect("UserPage.aspx?user=" + HttpUtility.UrlEncode(txtUser.Text));
And then retrieve it via:
string user = Request.QueryString["user"];
If you're worried about users messing with a query string (be sure to validate it), you could also store a Session variable before doing the redirect.
warning: this is a gross but easy solution
Session("myTextbox")= txtUser.Text
this will persist the value so on the page_load of the next page you can say
txtUser.Text=Session("myTextBox")
What are you passing form page to page? Is it a list of things. You could have an object with different properties and could then pass it through a session. If you have multiple pages I would suggest doing this if you could end up reusing it else where. Passing it through the url, you would then need to validate it, because if someone types the url with the correct information or something that is being directly input into a database they could cause problems and/or unexpected results.

Resources