0

the url on my local project is :-

http://localhost/mysite/KB/Type-1/General-Article 

and on server it is:-

http://www.mysite.com/KB/Type-1/General-Article 

from both the urls, I want to fetch the URL part until Type-1 without slash

ie from localhost website address, I want:-

http://localhost/mysite/KB/Type-1 

and from server, I want :-

http://www.mysite.com/KB/Type-1 

How can I do this? Please help ..thanks

Please note that Type-1 is not a fixed, it wcan change. It can be anything like "Type-2", "User-Articles", etc.

Thanks for the answers but will this still work if there is a slash at the end of the URL too? like this:- http://www.mysite.com/KB/Type-1/General-Article/ Please note that the text "KB" in the URL is FIXED.

3 Answers 3

2

try this code,

url.Substring(0, url.LastIndexOf("/")-1); 

edit: if you have ending slash, you can use following code

url=url.Remove(test1.Length - 1); url.Substring(0, url.LastIndexOf("/")-1); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer but will this still work if there is a slash at the end of the URL too? like this:- "mysite.com/KB/Type-1/General-Article" Please note that the text "KB" in the URL is FIXED.
2

since you don't provide enough information only some general idea:

EDIT 2 - as per comments:

string MySubURL = MyURL.SubString ( 0, MyURL.TrimEnd(null).TrimEnd(new char[]{'/'}).LastIndexOf ("/") ); 

6 Comments

Thanks for the answer but will this still work if there is a slash at the end of the URL too? like this:- mysite.com/KB/Type-1/General-Article Please note that the text "KB" in the URL is FIXED.
I am fetching the URL using this string MyURL = HttpContext.Current.Request.RawUrl; The problem is that on my local it is returning "mysite/KB/Type-1/General-Article " so the result of your code is not correct for me. How do I get the complete URL from the address bar?
/mysite/KB/User-Artic this is what I got as a result
ok I just added static URL value in the myurl string and still the result is the same
you don't get the exact URL from the address bar except by using javascript on the client side.
|
1

This will do what you want whether there is a '/' at the end or not in a single line. It is a bit longer, but it works for all of your outlined requirements.

string url = "http://www.mysite.com/KB/Type-1/General-Article"; string seg = url.EndsWith("/") ? url.Substring(0, url.TrimEnd('/').LastIndexOf('/')) : url.Substring(0, url.LastIndexOf('/')); 

1 Comment

OP specifically says that "Type-1" can't be expected

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.