Monday, June 18, 2007

Converting a String value to a URI object instance

I came across a situation in .NET where I needed to programatically set the 'ReportServerUrl' property for a ReportViewer object in my C# code-behind. This was mostly due to the fact the application can be deployed on several different servers of which the I placed the URL to the Report Server in web.config.

So, I thought that I could simply implement:
ReportViewer1.ServerReport.ReportServerUrl =


I learned that ReportServerUrl needed to be assigned to a System.Uri object. So, I came up with the following function to convert a given string to a Uri object:

public Uri NavigateTo(string location)
return new Uri(location);

So, now all I need to do is:

1) Put URL into web.config
<add key="reportserver_url" value="http://localhost/reportserver"/>

2) Write a public interface to the configuration string

public static string GetReportServerUrl()
return AppConfig.ReportServerUrl;

3) Assign property in code-behind
ReportViewer1.ServerReport.ReportServerUrl = NavigateTo(AppConfig.GetReportServerUrl());

No comments: