Thursday, 26 August 2010

Randomly sort a list using LINQ

This is far neater than the loop and temporary list I had in mind. Two lines of code!
Random rnd = new Random();
myList = myList.OrderBy(x => rnd.Next()).ToList();
Or, to take n random elements from a list:
Random rnd = new Random(); 
myList = myList.OrderBy(x => rnd.Next()).Take(n).ToList();

Wednesday, 25 August 2010

Looking for freelance .net web developers?

Due to the wonders of SEO I get quite a few emails from employers and agencies looking for freelance .net developers. If you're looking for .net developers, developers, .net web developers, you should try these:
  • ask me if I'm available :)
  • ask me if I know anyone available.
  • advertise on Monster for £99 . This really is very cheap, and will elicit lots of responses. You just need the time to sort through all the cv's you receive. Be sure to state 'no agencies' and stipulate that you need a valid work permit, otherwise you'll get a whole heap of hopefuls who don't yet have the right to work in your country.
  • if you're in Bristol, check out the Bristol Media Talent Retention Scheme . If you're not in Bristol, then look for similar industry-community groups in your area.

Tuesday, 24 August 2010

Run code only in debug mode

I always forget the syntax for this!
   //do this in debug
   //do something else in release

Thursday, 19 August 2010

Binding a dropdown list to an XML file of countries

using System.Xml; 

protected void Page_Load(object sender, EventArgs e)
   if (!Page.IsPostBack)

private void Bind_ddlCountries()
   XmlDocument doc = new XmlDocument();

   foreach (XmlNode node in doc.SelectNodes("//country"))
      ddlCountries.Items.Add(new ListItem(node.InnerText, node.InnerText));
and the source xml country list should look like this:
You can get the full list of countries I most recently used here.

Wednesday, 18 August 2010

Empty title tag added when adding an HtmlTitle into a placeholder

I just spent too much time trying to work out why the HEAD tag of a site I was given contained two TITLE entries, and therefore wasn't validating. The head of the masterpage contained a user control that consisted of a single placeholder, to which was being dynamically added a title control like so:

HtmlTitle title = new HtmlTitle();
title.Text = Title;

where the variable Title is a string value sent up from specific content page.

It turned out that because the master page's HEAD markup didn't contain any TITLE tags, ASP.NET was surreptitously adding one for me despite me adding one programmatically during the page's build. Stepping through the code when the page was loading didn't show this. It just kind of did it.

I found this hack to put a top to this strange behaviour. I put this in the HEAD tag on the master page. I'm adding the tags to keep it happy, but setting it to visible="false" so we don't see it:
<title id="Title1" visible="false" runat="server"><%-- hack to turn the auto title off --%></title>
Dirty but effective.

Monday, 16 August 2010

Get physical path of file in EpiServer from unfied file path

UnifiedFile file = HostingEnvironment.VirtualPathProvider.GetFile(path) as UnifiedFile;
string filePath = file.LocalPath;

Wednesday, 4 August 2010

Getting parameters from RawUrl

Most of the sites I'm working on use URL re-writing (URLRW for short), or "vanity/friendly URLs" as some SEO bods call them. Also, most of the sites I'm working on have affiliate schemes and PPC schemes sending traffic to the sites.

The URLRW rules turn the provided Url into one with appended parameters to identify specfic content. For example:

Vanity Url displayed in browser:
Url after rewriting:
Picking up the parameters in .Net is easy:

Things get a bit more complex when parameters need to be appended to the vanity Url and recognised by your code. PPC traffic to your yellow widgets might be sent to:
This Url is re-written by your URLRW rules, and so you can't grab the utm_x parameters using Request.QueryString["utm_x"]. We can however access the 'unfriendly' Url using
which using the above example will give us the string
but grabbing the parameter values with ease doesn't seem quite so obvious. We could write some kind of bespoke method to parse through the string to find our params and value. The good news is we don't have to. Using the following code you can take the 'real' or 'unfriendly' Url string, and grab the parameter values with ease:
Uri theRealURL = new Uri(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.RawUrl);

string utm_source = HttpUtility.ParseQueryString(theRealURL.Query).Get("utm_source");

string utm_medium = HttpUtility.ParseQueryString(theRealURL.Query).Get("utm_medium");

string utm_campaign = HttpUtility.ParseQueryString(theRealURL.Query).Get("utm_campaign");