QueryString converted to URL-Encode using NameValueCollection - c#

I am passing an encrypted URL string:
Default.aspx?S3tLlnIKKzE%3d
I want to pass that URL string back into the ASPX page in a variable.
protected string qs = string.Empty;
NameValueCollection qscollstring = HttpContext.Current.Request.QueryString;
qs = qscollstring[0];
Which return : S3tLlnIKKzE=
The value in qscollstring[0] is correct: S3tLlnIKKzE%3d
I understand the problem is URL-Encoding, but I cannot find a way to keep the string as is.
It seems that assigning the value from qscollstring[0] is: S3tLlnIKKzE%3d
to string changes the value : S3tLlnIKKzE=
I need to to stay: S3tLlnIKKzE%3d

Use HttpUtility.UrlEncode method to encode the string.
qs =HttpUtility.UrlEncode(qscollstring[0]);

You can also pull the value from the Uri of the current URL without having to Encode the value.
Sample:
Uri u = new Uri("http://localhost.com/default.aspx?S3tLlnIKKzE%3d");
string q = u.Query;
And part of your page:
string q = !String.IsNullOrEmpty(Request.Url.Query) && Request.Url.Query.Length > 1 ? Request.Url.Query.Substring(1) : Request.Url.Query;

Like me if you are searching for the reverse .. use
qs =HttpUtility.UrlDecode("S3tLlnIKKzE%3d");
to get back S3tLlnIKKzE=

Related

How to get a parameter from a URL encoded?

I have the following code to get parameters and their key from a URL:
string queryString = new Uri(URL).Query;
var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);
var paramsList = new Dictionary<string, string>();
foreach (var parameter in queryDictionary)
{
var key = (string)parameter;
var value = System.Web.HttpUtility.ParseQueryString(queryString).Get(key);
}
It works perfect. Exception, in value, I have the decoded value. I would need to have it as it was in the address, before it was decoded. How can I do it? Note that to encoded it after gives different value in some cases.
The encoding is happening in your first line already:
string queryString = new Uri(URL).Query;
Presumably you want to avoid writing your own code to extract the Query part from a URL. Which is sensible. You can still rely on the Uri class to do the parsing for you:
var uri=new Uri(URL);
var queryString= URL.Replace(uri.GetLeftPart(UriPartial.Path), "").TrimStart('?');
———————————————————————————————————
(
var URL="http://a/b/?d= #£$%% sdf we 456 7 5?367";
var uri=new Uri(URL);
Console.WriteLine(uri.Query); // Already url-encoded by the Uri constructor
var queryString = URL.Replace(uri.GetLeftPart(UriPartial.Path), "").TrimStart('?');
Console.WriteLine(System.Web.HttpUtility.ParseQueryString(queryString)); //Not encoded!
)
(As an aside, LinqPad helps)
A possible way of doing this is to encode it back:
var encodedValue = HttpUtility.UrlEncode(value);
ASP.NET automatically calls HttpUtility.UrlDecode when you access the query string.
This is a bit dirty, but should work fine.

Get Querystring value using c#

I am using the below code to assign the iframe src through javascript.It's working fine. But in c# code behind i got the query string like this. id=Y&amp%3bcust_id=100&amp%3. How can i reduce this.Now
var value = "validity.aspx?id=Y&cust_id=" + cust_id + "";
frameElement.src = value
i want to get the value of customer from query string but it always return null.
if(Request.QueryString["cust_id"] !=null) //It returns null
You need to encode your "&" caracters. For some reason, the server is reading them as &amp%3b.
Try this:
var value = "validity.aspx?id=Y&cust_id=" + cust_id + "";
value = encodeURIComponent(value);
frameElement.src = value
You need to decode the URL using HttpUtility.HtmlDecode
Try something like this:
var value = HttpUtility.HtmlDecode(URL);

c# rewrite url parameter

I have a simple task without a simple solution.
I have a parameter in the browser that needs to be changed or rewritten
for instance www.contoso.com/countries.aspx?country=UK
all I need is to rewrite the parameter without checking the url so it might appear as:
www.contoso.com/countries.aspx?country=France
I have tried something like that but with no joy
string parameter2 = Request.QueryString["country"];
Context.RewritePath(parameter2.Replace("?country=", "France"));
You could do something like this:
var url = "www.contoso.com/countries.aspx?country={0}";
var country = "UK";
url = String.Format(url, country);
Alternatively you can do:
var url = Request.Url.AbsolutePath;
var country = Request.QueryString["country"];
url = url.Replace(country, "UK");
Then:
Response.Redirect(url);
Can you not read the whole URL into a string, split it on the '?' and then add your new bit to the first part of the string?
Something like this:
var url = Request.QueryString;
var newUrl = url.split('?');
url = newUrl[0] + "?country=France";
I dont know if that will work, its just a thought
If you want to replace the complete querystring, use
newVal = string.LastIndexOf("?");
and then
URL.Replace(oldVal, newVal);
OR if you have just one parameter in querystring and want to replace only value of it, use
newVal = string.LastIndexOf("=");
URL.Replace(oldVal, newVal);
Look at this detailed response for solution to your problem.

Get url parts without host

I have a url like this :
http://www.somesite.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye.
I want to get mypage.aspx?myvalue1=hello&myvalue2=goodbye from it . Can you tell me how can I get it ?
Like this:
new Uri(someString).PathAndQuery
var uri = new Uri("http://www.somesite.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string pathOnly = uri.LocalPath; // "/mypage.aspx"
string queryOnly = uri.Query; // "?myvalue1=hello&myvalue2=goodbye"
string pathAndQuery = uri.PathAndQuery; // "/mypage.aspx?myvalue1=hello&myvalue2=goodbye"
Place your string URL into a URI object and then use the AbsolutePath & Query properties to get the URL parts you need.
Or use the PathAndQuery property to get both, which is what you need.
More information can be found here:
http://msdn.microsoft.com/en-us/library/system.uri_members%28v=VS.71%29.aspx
new Uri(System.AppDomain.CurrentDomain.BaseDirectory).Segments

How to append parameter?

How can I get a string by appending parameter(s) to Request.Url.Query?
Let say I have a parameter "value=100"
Request.Url.Query After Appending
"" "?value=100"
"?" "?value=100"
"?page=15" "?page=15&value=100"
"?page=15&sort=col" "?page=15&sort=col&value=100"
You cannot append parameters to the current query string. The querystring is readonly. Now if you want to manipulate querystrings in your application you could use the Url helpers to generate and manipulate urls.
You could also checkout the ParseQueryString method but this is rarely useful in an ASP.NET MVC application where you have routes and url helpers.
Sample usage:
string query = "?page=15&sort=col";
var values = HttpUtility.ParseQueryString(query);
values["value"] = "100";
query = values.ToString(); // page=15&sort=col&value=100

Categories