-1

Suppose We are on the page www.abc.com/apple-store

then how to get string apple-store in asp C# code.

to store into another variable.

Tim
  • 27,313
  • 8
  • 59
  • 71
Rohit
  • 1
  • 3

3 Answers3

1

You should use the Request.RawUrl property. See more details here.

Alternatelly you can also use the Request.Url (see here) property to get different parts of the current URL. For example you will get the same result using Request.Url.LocalPath.

SmartDev
  • 2,681
  • 1
  • 12
  • 18
  • And then parse that as per this answer: http://stackoverflow.com/questions/14685147/how-can-i-parse-http-urls-in-c – LDJ Nov 07 '14 at 08:58
  • @ SmartDev What is 'sw' . in link you have given mentioned this. i can't figure it. sw.WriteLine(Server.HtmlEncode(Request.RawUrl)); The following code example uses the HtmlEncode method to HTML-encode the value of the RawUrl property and the WriteLine method to write the encoded value to the file. This code example is part of a larger example provided for the HttpRequest class. – Rohit Nov 07 '14 at 09:02
  • `sw` it's a `StreamWriter`. The example uses it to write the result in a file. You don't need this. Just use `string url = Request.RawUrl;` – SmartDev Nov 07 '14 at 09:07
1

You can use string.last() to extract it.

string lastPartUrl =HttpContext.Current.Request.Url.AbsoluteUri.Split('/').Last();
TheProvost
  • 1,652
  • 1
  • 13
  • 36
0

You can get the url in a string variable. Further you can implement the below logic which will save the value in a variable.

string str = "www.abc.com/apple-store";
string result = "";
int i= 0;
int len = str.Length;

//Get the index of the character
i = str.IndexOf('/');    
//store the result in the variable
result = str.Substring(i+1,len-i-1);

Console.WriteLine("Resultant:- {0}", result);`

Hope this helps a bit.