Silverlight: Accessing files outside of the ClientBin directory of your website using a relative URI


Accessing an arbitrary file on your website is a very common need in RIA scenarios. This is easily done from Silverlight when the files you are trying to access are under the ClientBin directory, or given an absolute URI path to the targeted file. Unfortunately, the ../ notation in Silverlight URIs  does not go up from the ClientBin directory.


Let’s say you want to access MyRobots.jpg from your site’s Images directory :


image


As previously stated, using ../ or just / in Silverlight 3 will only go up to the ClientBin directory. The only way to gain access to the image is to use an absolute URI in the form of http://www.abc.com/mysite/Images/MyRobots.jpg, which you probably do not want as moving your application to another site would require a recompilation of your program.


The solution is to determine at runtime the absolute path of your application, and use it as a prefix to the resources you want to access. You can get your application’s absolute URI using the following code:



Application.Current.Host.Source.AbsoluteUri


Just remove the ClientBin part and you are ready to go:



Regex.Replace(Application.Current.Host.Source.AbsoluteUri, @”ClientBin/[\w/.]*”, string.Empty);


As usual, an example project is attached, where I make use of this technique in a Binding with a Converter and a static property.



<Image Source=”{Binding Converter={StaticResource WebSiteRelativeURIConverter},ConverterParameter=Images/,Path=MyImageFilename/>


image


Have fun!

RelativeExternalUris_en.zip

Comments (2)

  1. the mainpage show wrong says:

    at RelativeExternalUris.WebSiteRelativeURIConverter.get_AbsoluteSitePrefix() in D:DownLoadRelativeExternalUris_enRelativeExternalUrisRelativeExternalUrisWebSiteRelativeURIConverter.cs:line 21

      at RelativeExternalUris.WebSiteRelativeURIConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture) in D:DownLoadRelativeExternalUris_enRelativeExternalUrisRelativeExternalUrisWebSiteRelativeURIConverter.cs:line 25

      at System.Windows.Data.BindingExpression.ConvertToTarget(Object value)

      at System.Windows.Data.BindingExpression.GetValue(DependencyObject d, DependencyProperty dp)

      at System.Windows.DependencyObject.EvaluateExpression(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry)

      at System.Windows.DependencyObject.EvaluateBaseValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)

      at System.Windows.DependencyObject.EvaluateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry newEntry, ValueOperation operation)

      at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)

      at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)

      at System.Windows.Data.BindingExpression.SendDataToTarget()

      at System.Windows.Data.BindingExpression.SourceAcquired()

      at System.Windows.Data.BindingExpression.TargetLoaded(Object o, RoutedEventArgs e)

      at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)

      at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

  2. if url https then its not work