2

在 Silverlight 应用程序中,我有时需要连接到托管应用程序的网站。为了避免在我的 Silverlight 应用程序中对网站进行硬编码,我使用如下代码:

WebClient webClient = new WebClient();
Uri baseUri = new Uri(webClient.BaseAddress);
UriBuilder uriBuilder = new UriBuilder(baseUri.Scheme, baseUri.Host, baseUri.Port);
// Continue building the URL ...

创建一个WebClient实例只是为了访问 XAP 文件的 URL 感觉非常笨拙。有没有其他选择?

4

4 回答 4

9

Application.Current.Host.Source检索 XAP 的 URI。

于 2009-07-24T14:57:53.387 回答
9

我用,

Uri baseUri = new Uri(Application.Current.Host.Source, "/");
// Example results:
//  http://www.example.com:42/
//  or
//  https://www.example.com/

无需字符串解析!您还可以使用此方法创建完整的 Url,例如,

Uri logoImageUri = new Uri(Application.Current.Host.Source, "/images/logo.jpg");
// Example result:
//  http://www.example.com/images/logo.jpg
于 2009-10-30T01:52:54.557 回答
0

这将在 ASP.NET 中构建根 URL。然后,您需要通过 Silverlight 的 InitParams 传入 baseUrl 并添加“ClientBin\silverlight.xap”。

// assemble the root web site path
var baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd ('/') + '/';
于 2009-07-24T15:03:43.003 回答
0

就我而言,我不在主文件夹中工作。我在 h||p://localhost:1234/子文件夹中工作。这在 Visual Studio IDE 中工作时没有问题。但是当移动到服务器时它失败了。以下几行

Application.Current.Host.Source

可以通过公共函数替换,结果如下:

Public Sub AppPathWeb()
    Res = Application.Current.Host.Source.AbsoluteUri.Substring(0, Application.Current.Host.Source.AbsoluteUri.LastIndexOf("/") + 1)
    Return New Uri(Res) 
End Sub

结果,我可以像这样捕获我的文件

MyImage = New Uri(AppPathWeb, "HelloWorld.jpg")

结果是,在服务器上,url 转到 h||p://mydomain.com/mysubfolder/HelloWorld.jpg"

祝你好运

Goldengel.ch

于 2010-06-29T14:45:57.593 回答