Blog posts


  • How to combine URLs/URIs in C#

    February 03, 2014 | Tags: C#, Snippet
    The file system have the Path.Combine method to combine paths but how to combine a base URL with an absolute or relative URL/URI?

    The answer is to use the System.Uri-constructor to combine the URL:

    public static Uri CombineUri(string baseUri, string relativeOrAbsoluteUri) {
    return new Uri(new Uri(baseUri), relativeOrAbsoluteUri);
    }

    public static string CombineUriToString(string baseUri, string relativeOrAbsoluteUri) {
    return new Uri(new Uri(baseUri), relativeOrAbsoluteUri).ToString();
    }

    ..

    // Results in "http://www.my.domain/relative/path"
    var a = CombineUriToString("http://www.my.domain/", "relative/path");

    // Results in "http://www.my.domain/absolute/path"
    var b = CombineUriToString("http://www.my.domain/something/other", "/absolute/path");


  • Convert JSON string to JavaScript objects safely

    February 03, 2014 | Tags: Javascript, JSON, jQuery
    If you have a JSON string that you want to convert into JavaScript object but doesn't want to use eval (which you should avoid due to the security implications), you can either use jQuery.parseJSON() or - if you're not using jQuery - use a dedicated JSON library such as JSON 3.
    // using jQuery
    var myObject = jQuery.parseJSON(myJSONString);

    // using JSON 3 (for polyfill in older browsers)
    var myObject2 = JSON.parse(myJSONString);

  • Passing arguments to another javascript function

    September 30, 2013 | Tags: Javascript, Snippet
    If you which to forward a call from one function to another passing all the parameters which were sent to the first function this can be done by using apply and passing arguments:
    function myFunction(args){
    myOtherFunction.apply(this, arguments);
    }

    function myOtherFunction(fieldA, fieldB){
    alert(fieldA + " " + fieldB);
    }

    myFunction('hello','world');

    Please note!The call to apply uses arguments and not args. This is intended and not a typo. The parameter args allows the function myFunction to receive arguments. The arguments object is an array of all the parameters sent to the function. If you don't want to specify which parameters myOtherFunction takes you can use arguments[0] etc in order to access the posted parameters.


  • C# test if object or type implements interface

    September 29, 2013 | Tags: C#, Snippet
    If you have a type or an instance you can easily check if they support a specific interface.

    To test if an object implements a certain interface:
    if(myObject is IMyInterface) {
    // object myObject implements IMyInterface
    }

    To test if a type implements a certain interface:
    if(typeof(IMyInterface).IsAssignableFrom(typeof(MyType))) {
    // type MyType implements IMyInterface
    }

    If you got a generic object and want to do a cast as well as a check if the interface you cast to is implemented the code is:

    var myCastedObject = myObject as IMyInterface;

    if(myCastedObject != null) {
    // object myObject implements IMyInterface
    }


  • Batch download from HTTP directory listing

    August 08, 2013 | Tags: Tools, Open source, HTML

    Want to download a number of files that are exposed through directory listings via HTTP? It can be really time consuming to do manually, especially if it also includes sub folders. Thankfully there's the tool Wget to the rescue! And these are the steps to follow:

    Download and extract GNU Wget:
    http://www.gnu.org/software/wget/

    Launch a command line prompt (ie run "cmd") and enter the following command (replacing "http://domain/folder/" with the url you want to download from:
    wget --wait=1 --recursive --no-parent --reject "index.html*" "http://domain/folder/"
    ..or the same but with parameters in short-hand:
    wget -w1 -r -np -R "index.html*" "http://domain/folder/"

    Explanation of parameters

    -w1, --wait=1 adds a 1 second wait between requests out of courtesy
    -r, --recursive do a recursive request (follow links)
    -np, --no-parent don't follow links backwards
    -R "index.html*", --reject "index.html*" don't save any folder listings, just the files.