views:

625

answers:

2

I am trying to retrieve query string values from a url. And the app should be a silverlight app.

For Eg: The sample URL might look like http://<hostname>/silverlightApp/Default.aspx?S=Name|address|title|sal|...

I should be able to take the query string and built a Silverlight UI.

Can this be done or Silverlight is not a good candidate for this type.

A: 

See System.Web.HttpUtility.ParseQueryString() Method, which parses a query string into a NameValueCollection.

[Later] Sorry, the Silverlight runtime seems to be without the System.Web namespace.

See system.Uri.Query in the System namespace provided with Silverlight runtime.

The Query property contains any query information included in the URI. Query information is separated from the path information by a question mark (?) and continues to the end of the URI. The query information returned includes the leading question mark.

The query information is escaped according to RFC 3986.

The following example writes the query ?date= today to the console.

Uri baseUri = new Uri ("http://www.contoso.com/");
Uri myUri = new Uri (baseUri, "catalog/shownew.htm?date=today");

outputBlock.Text += "Uri.Query: ";
outputBlock.Text += myUri.Query;
outputBlock.Text += "\n";
gimel
Can I use this within the SilverLight or in SilverLight Web Project? Slightly new to this, excuse me for not using the right terms.thanks
Ranjit
Thank a bunch you gimel. I will try it out and post the result. Right now I am trying to get POCLR objects/classes to bind to basic controls(textblock, list) in SilverLight via WCF.thanks again....
Ranjit
A: 

There are multiple ways you can do this. In the hosting page, you can pull out query string values using Request.QueryString, and then pass them to Silverlight using the initParams tag, i.e.:

    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
        width="100%" height="100%">
        <param name="source" value="/ClientBin/MyApplication.xap" />
        <param name="onerror" value="onSilverlightError" />
        <param name="background" value="white" />
        <param name="minRuntimeVersion" value="3.0.40620.0" />
        <param name="autoUpgrade" value="true" />
        <param name="windowless" value="true" />
        <param name="initParams" value="<%=InitParameters %>" />
        <param name="splashScreenSource" value="<%=SplashScreenSource %>" />
        <a href="http://go.microsoft.com/fwlink/?LinkID=149156&amp;v=3.0.40620.0" style="text-decoration: none;">
            <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
                style="border-style: none" />
        </a>
    </object>

Or from within the Silverlight application itself, you can grab the querystring and other parts of the URI by using HtmlPage.Document.documentUri, e.g.:

Uri uri = HtmlPage.Document.DocumentUri;

And once you've got the actual querystring, you can parse it using regular expressions, or whatever your poison of choice happens to be.

HTH.

Ken Smith