Wednesday, June 8, 2011

Circumventing the uriMappings in Silverlight

Redirection chaosI created a Silverlight application with the Silverlight Navigation project template that needed to open a link to a PDF file located in a subfolder of the host web application directory. The  project has a convenient uriMapping process defined in the MainPage.xaml XAML to provide search engine friendly links to the various Silverlight pages. It is located in the Windows.System.Navigation namespace. It looks something like this:

<uriMapper:UriMapper>
   <uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
   <uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
</uriMapper:UriMapper>

 

I needed to link to an absolute Url, not a uri relative to the Silverlight app, like http://www.deanwillson.com/downloads/Continuous-Improvement-professional-Org-1st-Edition.pdf, but if the uriMapper catches that link it attempts to re-map it to a resource that doesn’t exist which generates the following error:


“Content for the URI cannot be loaded. The URI may be Invalid”


Solution


In order to accomplish linking outside of the defined uriMappings, I added the link in the XAML of the HyperlinkButton NavigateUri.



<HyperlinkButton Name="lnkReport" NavigateUri="http://www.deanwillson.com/downloads/Continuous-Improvement-professional-Org-1st-Edition.pdf" Content="Click here to open your document" TargetName="_blank" Margin="116,11,184,10" Width="200" />

All this probably looks pretty obvious, but it started with me trying to open the pdf report using code-behind in the clicked event handler for the button, which kept throwing the Invalid Uri exception. I even added a special uriMapping that mapped to the same link, which also did not work.


photo credit: eirikref / CC BY 2.0

No comments: