2009-10-30 29 views

回答

8

Uri类有乌里忙玲一些真正有用的方法 - 包括Uri.TryCreate。

具体而言,GetComponents方法可能会对您有所帮助。

+0

良好的信息,但没有找到可以帮助我的东西。 – Manish 2009-10-30 13:31:08

0
string myString = "http://www.abc.com/123/product/234?productid=123"; 
bool contains = myString.Contains("http://www.abc.com/123"); 
+0

我想写一个通用代码!!!我希望这是自我模式。这仅仅是一个例子,请不要用例子中提供的值来思考一般情况。 – Manish 2009-10-30 11:48:26

+0

如果我尝试http://www.xyz.com/http://www.abc.com/123,该怎么办?那仍然通过测试! – 2009-10-30 12:32:20

+0

你是什么意思推广它? – 2009-10-30 12:35:24

5

试试这个:

Application.Current.Host.Source.AbsoluteUri 

这将使链接到你的.xap文件。 你将不得不替换你的.xap路径,你有你的应用程序uri。

Application.Current.Host.Source.AbsoluteUri.Replace(@"ClientBin/MySilverlight.xap", ""); 
+2

您也可以对该字符串进行非硬编码。将其替换为'Application.Current.Host.Source.AbsolutePath'值... – felickz 2011-09-13 19:33:44

0

你可以使用HtmlPage.Document.DocumentUri(这部作品在* .xaml.cs文件)

2

我选择了一个完全通用的解决方案目前的URI:

/// <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); 
     } 
    } 
+0

我觉得这需要抛出一个异常而不是返回一个伪造的字符串。 – theMayer 2013-06-17 02:29:41