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)
Related
I have this url (www.site.com/folder1/sub-folder).
Now I just want to get only folder1 from this url. How can I do that?
I'm getting Last root url from below code but I want only first root url.
string s = Page.Request.Url.AbsolutePath;
s = s.Substring(s.LastIndexOf("/")+1);
Please help me to get only first directory value.
Try this:
Uri uri = new Uri("http://www.example.com/folder1/sub-folder");
var segs = uri.Segments;
var folder = segs[1];
You can use Split() method twice like
string str = "www.site.com/folder1/sub-folder";
string folder = (str.Split('/')[1]).Split('/')[0];
You can use Split like:
var value = s.Split(new[]{'/'}, StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault();
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=
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 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