How to get a certain part of current URL in silverlight?
I need a certain part of current URL.
Sa开发者_运维问答y for example the URL is: http://www.abc.com/123/product/234?productid=123 And I want to check if a certain string contains http://www.abc.com/123Please don't give answers like "do string manipulation" and all. Is there a way to get this sort of URL?
The Uri class has some really helpful methods for Uri mangling - including Uri.TryCreate.
Specifically, the GetComponents method might help you.
Try this:
Application.Current.Host.Source.AbsoluteUri
That will give the url to your .xap file. You will have to replace your .xap path and you have your application uri.
Application.Current.Host.Source.AbsoluteUri.Replace(@"ClientBin/MySilverlight.xap", "");
I opted for a completely generic solution:
/// <summary>
/// Get the site URL (one step up from ClientBin)
/// </summary>
public string HostWebSite
{
get
{
string host = App.Current.Host.Source.AbsoluteUri;
int clientBin = host.IndexOf("ClientBin", 0);
if (clientBin == -1)
return "Could not find \"ClientBin\" in " + host;
return host.Substring(0, clientBin);
}
}
string myString = "http://www.abc.com/123/product/234?productid=123";
bool contains = myString.Contains("http://www.abc.com/123");
You can get the current uri using HtmlPage.Document.DocumentUri (this works in *.xaml.cs files)
精彩评论