Reputation: 46651
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.
Upvotes: 0
Views: 196
Reputation: 217411
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"
.
Upvotes: 3
Reputation: 1960
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()
Upvotes: 1
Reputation: 21674
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");
}
Upvotes: 0
Reputation: 158399
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
}
Upvotes: 0
Reputation: 6867
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.
Upvotes: 2