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
Related
How to get current page URL excluding query string parameters.
My URL is like this https://mywebsite.com/myproject/detailspage?id=2812
I want to get only https://mywebsite.com/myproject/detailspage.
I tried this but not helped.
string path = HttpContext.Current.Request.Url.AbsolutePath;
string url = HttpContext.Current.Request.Url.AbsoluteUri;
Please any suggestions.
Request.Url.GetLeftPart(UriPartial.Path)
My link is:
http://excample.com/default.aspx?param=1
I want to get "1" in link. And if my link is:
http://excample.com/default.aspx?param1=1¶m2=0
Please help me to get the values of param1 and param2. Thank you my friend !!!
I use ASP.NET C#
You can try the below code.
Uri myUri = new Uri("http://excample.com/default.aspx?param1=1¶m2=0");
string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("param1");
OR
HttpContext.Current.Request.QueryString.Get("param1");
OR
Request.QueryString["param1"];
In every Request there are Form and QueryString properties.During the Request,in the Form property it contains the values which comes after submiting the form, and in QueryString it contains every parameter passed by the URL.So you need only get the QueryString from the Request and retrieve two parameters like this
var param1 = Request.QueryString["param1"]
var param2 = Request.QueryString["param2"]
You only think like this.Almost everything you need during the request is in the Request property.For parameters from query string they are in the QueryString property.
For deeply knowledge see here.https://msdn.microsoft.com/en-us/library/ms524784(v=vs.90).aspx and https://msdn.microsoft.com/en-us/library/ms525985(v=vs.90).aspx
You can try like this:
var uri = new Uri("http://excample.com/default.aspx?param=1");
var query = HttpUtility.ParseQueryString(uri.Query);
var par = query.Get("param");
or
var uri = new Uri("http://excample.com/default.aspx?param1=1¶m2=0");
var query = HttpUtility.ParseQueryString(uri.Query);
var par1 = query.Get("param1");
var par2 = query.Get("param2");
I am writing a backend for SlickGrid. I have an AJAX call that loads the column definitions from my MVC Controller. The controller looks like this:
Public ActionResult GetColumns(...){
List<object> columns;
foreach(var col in MyColumns){
columns.Add(new { id = col.id, name = col.name, width = col.width});
}
return Json(new { columns = columns });
}
One of the column attributes that slickgrid accepts is formatter, which takes a js function name.
Unfortunately, MVC puts that name in double quotes. I need to figure out if there is a way to serialize just that one field without quotes.
eg (this is what I want)
[{"id":1,"Name":"FirstName","width":50,"formatter":NameFormater},...]
vs (ths is what I am getting now)
[{"id":1,"Name":"FirstName","width":50,"formatter":"NameFormater"},...]
I know this isn't proper JSON Format to pass JS Code in it(Proper JS Format though). But I have a valid use case here and am trying not to add too much complexity.
You could wrap the word NameFormater with special characters such as
[{"id":1,"Name":"FirstName","width":50,"formatter":"#NameFormater#"},...]
and when you get your results back, if you are using javascript
function(result){
result = result .replace(/"#/g, "");
result = result .replace(/#"/g, "");
// result now = [{"id":1,"Name":"FirstName","width":50,"formatter":NameFormater},...]
// ...Pass result to SlickGrid
}
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.
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=