tags:

views:

109

answers:

5

Lets say I have an absolute url /testserver/tools/search.aspx that I store in a variable url.

I want to check if url == /tools/search.aspx without having to use /testserver.

A more complete example would be:

My testserver contains the url http://www.testserver.com/tools/Search.aspx,

but my live server contains the url http://www.liveserver.com/tools/Search.aspx

If I compare a variable url which stores the testserver url to the liveserver url, it will fail, thats why I want to just check the /tools/Search.aspx portion.

+2  A: 
if (url.ToLower().Contains("/tools/search.aspx"))
{
   //do stuff here
}

I would use Contains in case you have a query string, but you could also use EndsWith("/tools/search.aspx") if you don't have query strings.

Chris Mullins
"EndsWith" might be a better choice.
Andrew Hare
Just curious, what makes EndsWith a better choice.
Xaisoft
@Andrew -- except in the case where you also want it to match .../search.aspx?name=something. EndsWith would be better if you were sure that you had the URL without the query parameters.
tvanfosson
@Xaisoft: I think (though I'm not sure) that the str1.EndsWith(str2) routine will look for a direct match against the last (str2.Length) characters of str1. str1.Contains(str2) has to search all of str1 to find any possible match.
Adam V
The case of "/tools/search.aspx.something" (not likely but possible).
cdm9002
Ok, this worked and it is is an option. Based on the other answers, is this the best one.
Xaisoft
It doesn't have anything after the aspx, so I guess EndsWith would be better right, is it more efficient?
Xaisoft
I ended up going with EndsWith as it works just fine.
Xaisoft
Since it is a url there is always the odd chance that the user added some random query string, just to try things out.
Fredrik Mörk
A: 

You can use the property AppRelativeCurrentExecutionFilePath, which will give you the path relative the application root. So "http://host/virtualfolder/page.aspx" will be "~/page.aspx":

if (HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.Equals("~/search.aspx", StringComparison.InvariantCultureIgnoreCase))
{
    // do something
}
Fredrik Mörk
+3  A: 

If your input is of the form "http://www.testserver.com/tools/Search.aspx":

var path1 = new Uri("http://www.testserver.com/tools/Search.aspx").AbsolutePath;
var path2 = new Uri("http://www.liveserver.com/tools/Search.aspx").AbsolutePath;

Both result in "/tools/Search.aspx".

Using the Uri is the best solution if you have to accept any URI, i.e. including those with a query string, fragment identifiers, etc.


If your input is of the form "/testserver.com/tools/Search.aspx" and you know that all input will always be of this form and valid and contain no other URI components:

var input = "/testserver.com/tools/Search.aspx";
var path1 = input.Substring(input.Index('/', 1));

Result is "/tools/Search.aspx".

dtb
Up voted because it, rather sensibly, suggests using tried and tested functionality that is provided by the core system libraries. Why would you do anything else?
belugabob
What happens if the application is deployed in a virtual folder on the server, as stated in the question?
Fredrik Mörk
+1  A: 
Regex.Match(url, @"^.*?/tools/search\.aspx\??.*",
                 RegexOptions.IgnoreCase).Success == true

If you grab url from Request.PathInfo you won't have the domain anyway...but you question is ambiguous, as you say your have a path /testserver/ in one but not in the urls you provide.

Otherwise, set url from Request.Url.ToString()

cdm9002
A: 

If the only difference is going to be the host part of the URL I would use the System.Uri class to compare their absolute paths (the "tools/Search.aspx" part of the uri).

Here's an example of how to do it:

static void Main(string[] args)
{
    //load up the uris
    Uri uri = new Uri("http://www.testserver.com/tools/search.aspx");            
    Uri matchingUri = new Uri("http://www.liveserver.com/tools/search.aspx");
    Uri nonMatchingUri = new Uri("http://www.liveserver.com/tools/cart.aspx");

    //demonstrate what happens when the uris match
    if (uri.AbsolutePath == matchingUri.AbsolutePath)
        Console.WriteLine("These match");
    //demonstrate what happens when the uris don't match
    if (uri.AbsolutePath != nonMatchingUri.AbsolutePath)
        Console.WriteLine("These do not match");
}
DoctaJonez