HttpRequest QueryValue extension methods

May 17th, 2008

Getting non string values from the querystring can be a chore if you need to read alot of values; parsing to the required type and ensuring the querystring value doesn’t cause an exception (”adsf” cannot become an integer) can add alot of code to each page. So a long while ago I created a small helper class to do this for me, which I’ve now upgraded (after reading a blog entry by ScottH about TypeConverters) to use generics and extension methods. I’ve also included methods to get a value from the Request.Form property.

 

using System;
using System.Web;
using System.ComponentModel;

namespace LukeSmith.Web
{
    public static class HttpRequestHelper
    {
        public static T? GetQueryValue<T>(this HttpRequest request, string name)
            where T : struct
        {
            return ConvertFromString<T>(request.QueryString[name]);
        }

        public static T? GetQueryValue<T>(this HttpRequest request, int index)
            where T : struct
        {
            return ConvertFromString<T>(request.QueryString[index]);
        }

        public static T? GetFormValue<T>(this HttpRequest request, string name)
            where T : struct
        {
            return ConvertFromString<T>(request.Form[name]);
        }

        public static T? GetFormValue<T>(this HttpRequest request, int index)
            where T : struct
        {
            return ConvertFromString<T>(request.Form[index]);
        }

        private static T? ConvertFromString<T>(string value)
            where T : struct
        {
            T? result = null;

            if (!string.IsNullOrEmpty(value))
            {
                TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));

                if (converter.CanConvertFrom(typeof(string)))
                {
                    try
                    {
                        object obj = converter.ConvertFromInvariantString(value);
                        result = (T)obj;
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            return result;
        }
    }
}

 

Get meshed up

April 26th, 2008

If you follow Microsoft technology, then unless you’ve been hidden under a rock for the past week or two, you’ve probably heard of Live Mesh. ScottIsAFool sent me an invite the other day so I’ve got it in. If you’d like to get in then checkout ShareMesh, it’s an invite sharing site since every Mesh user gets 2 invites.

What is Mesh?

Mesh is a platform for making your data available across all your devices; whether it’s your Desktop, Laptop, Mac or Mobile (coming soon). The Mesh application allows you to share folders and files on your machine across all your devices and the Mesh Live Desktop, as well as with friends, and Mesh keeps everything synched.

LiveSide have all the information you need.

Videos

Technorati Tags:

Send to Nabaztag IE Extension

April 23rd, 2008

If you’re a long term blog reader of mine then you’d know I have one of those pointlessly cool WiFi Nabaztag rabbits. It usually just sits on my shelf doing nothing, but I’ve now decided to use it as a podcast player. I created a simple and quick IE Extension to be able to send links (the rabbit will only play mp3’s) to the Nabaztag Api.

If you have a Nabaztag and you too want to send URLs from IE to be played on your pointlessly cool rabbit as well then follow the instructions:

  1. Download and extract the SendToNabaztagIEExtension zip to somewhere on your machine.
  2. Right click on IEExtension.reg and select edit.
  3. Modify the directory of IEExtension.html and save.
  4. Double click IEExtension.reg to install the registry settings. (note: usual blurb about the dangers of editing the registry)
  5. Edit IEExtension.html and enter the token and serial number for your rabbit. These can be found under the "my Burrow" section of the Nabaztag website.
  6. Close any instances of IE.

Next time you open IE, when right clicking on a hyperlink you will see an option ‘Send to Nabaztag’. Clicking this will send the link to your bunny (note: the url is sent regardless of whether the rabbit can actually play it).

sendtonabaztag

Notes: A new window is opened to make the request, which will show the response from the NabaztagApi. I tried using XmlHttpRequest but was unable to because of the cross-domain call being made. It was only quickly put together, so use at your own risk!

Technorati Tags:

Blogging photography

April 15th, 2008

I was reading over on the Digital Photography School blog about blogging your photos. Well I have a blog, and I’ve recently started taking photos with a Canon 400D DSLR I bought last year (thanks to the tax man who over taxed me years ago and me for finally asking for it back), but I haven’t really merged the two. I upload photos to my flickr account, but don’t link to them from my blog. Until now…let’s hope I keep it up.

Heres a photo of the sun setting in front of the main stage at Reading Festival in 2007 (not with my DSLR). I remember getting quite burnt that year.

2007-08-27 002 034

Facebook applications

April 12th, 2008

Theres probably people with far more than this added to their profile. My PC struggles scrolling their profile page!

facebook-apps 

 

On another note, SportsDo have released a Facebook application so you can automagically notify your friends when you upload a new track, and view your existing tracks from within Facebook.

Silverlight Chart Controls

April 7th, 2008

Some cool (and free) Silverlight charting controls.

Technorati Tags: ,

Someone should go BACK to the drawing board

April 1st, 2008

speech

edit: You can click the blue ‘back’ button in the top left, but it’s not intuitive. I was searching for a ‘back’ button next to the ‘Next’ and ‘Cancel’ buttons.

MSBuild Error when building VS2008 Projects

March 26th, 2008

I’ve been playing around with MSBuild and CruiseControl.NET and ran into a problem trying to build projects built using VS2008.

File format version is not recognized.  MSBuild can only read solution files between versions 7.0 and 9.0, inclusive.

Turned out I was targeting the v2.0.50727 version of MSBuild in the ccnet.config file

<tasks>
  <msbuild>
    <executable>C:WINDOWSMicrosoft.NETFrameworkv2.0.50727MSBuild.exe</executable>

Rather than the 3.5 version

<tasks>
  <msbuild>
    <executable>C:WindowsMicrosoft.NETFrameworkv3.5MSBuild.exe</executable>

Moved to WordPress

March 24th, 2008

It’s Easter Monday and I’ve been a bit bored (having just got back from an awesome weeks skiing in Meribel, France). I’ve decided to move away from the Dasblog blog engine to Wordpress. I’m very impressed so far. It’s very plugginable, and the default theme is by far better than what I previously had (for those who don’t register to my RSS feed).

It’s minus what?

March 12th, 2008

- what.