简体   繁体   中英

How to get a certain part of current URL in silverlight?

I need a certain part of current URL.
Say 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/123

Please 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");

您可以使用HtmlPage.Document.DocumentUri获取当前的uri(这适用于* .xaml.cs文件)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM