URL parameters in ClickOnce applications

by Mads Kristensen 17. May 2007 02:04

Some time ago, when I wrote the blog post importer for BlogEngine.NET, I wanted to be able to display different text in the ClickOnce application dependant on the user opening it directly from a web page. I did some digging and found out that ClickOnce supports query strings just like a website.

That made it possible to create a link similar to this:

BlogConverter.application?url=http://blog.madskristensen.dk/&username=my username

Enable URL parameters

In your ClickOnce project, right-click the project file in Solution Explorer and click Properties. Select the Publish tab and press the Options… button. Here you need to check the Allow URL parameters to be passed to application checkbox and hit the OK button.

Retrieve the parameters

The next thing was to find out how to retrieve the values of the query strings, because a ClickOnce application does not have an HttpContext so there is no Context.Request.QueryString property.

Instead, you need to retrieve the query strings from the application deployment context. I used this handy method, which returns a NameValueCollection of all the query strings.

using System.Deployment.Application;
using System.Web;
using System.Collections.Specialized;

private NameValueCollection GetQueryStringParameters()
{
  NameValueCollection col = new NameValueCollection();

  if (ApplicationDeployment.IsNetworkDeployed)
  {
    string queryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
    col = HttpUtility.ParseQueryString(queryString);
  }

  return col;

It’s pretty easy if you know where to look.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags: ,

Server-side

Comments

5/18/2007 1:35:50 AM #

dalager

I've been there, and yes it's tricky to get up and running.
For me it was even more challenging as my click-once app was a rich one-instance-only client, so in order to pass variables from the opening instance to the already open instance i had to use local remoting between instances.

dalager Denmark |

5/22/2007 12:54:14 AM #

lawrephord60

please replicate your blog as a message group in the msn groups and yahoo groups and pages at your site tripod.com and pages at your site bravenet.com

lawrephord60 Canada |

10/25/2007 11:58:05 PM #

sharon w

How do you call that function and get the value? I tried various ways e.g.
     Dim userName As String = nameValueTable("uid")
     Dim password As String = nameValueTable("pwd")

It just did not work.

Please let me know how you call that function and get value. Really appreciate it.

sharon w United States |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008