Thursday, November 28, 2013

ASP.NET MVC - Organizing your BundleConfig.cs

Here's how I organize my BundleConfig.cs in my ASP.NET MVC projects


First we have two static classes, "JavaScript" and "CSS".

These have both static sub-classes, "Bundles" and "CDN". The "Bundles" class contains const strings which are both keys for the bundle and the virtual path to the code location. The "CDN" class contains URLs.

public static class BundleConfig
    public static class JavaScript
        public static class Bundles

        public static class Cdn
    public static class Css{...}

For JavaScript, In the "Bundles" class, I keep my JavaScript in the folder "~/Scripts/" where jquery-2.0.3.js resides. Thus, the virtual path becomes "~/Scripts/jquery-2.0.3.js" and this text is contained in the string "Jquery".

public static class Bundles
    private const string PathBase = "~/Scripts/";
    public const string Jquery = PathBase + "jquery-2.0.3.js";

Below is the static class "CDN" which has a similar structure, which are just URLs that matches the above JavaScript classes. A CDN is very useful since it offloads loading time from your server, and data from the CDN can also run in parallel with loading the response from your server.

public static class Cdn
    public const string Jquery = "";

The "CSS" class functions similarly, with both a Bundle and a CDN class.

With this setup, registering the bundles becomes very easy and clean.

public static void RegisterBundles(BundleCollection bundles)
    bundles.Add(new ScriptBundle(JavaScript.Bundles.Jquery, JavaScript.Cdn.Jquery).Include(JavaScript.Bundles.Jquery));

Likewise, including the Bundle is also clean

Some additional tips:

Always place your javascript at the end of your <body> element, as it will block the rendering of the site if you place it above the content.

Monday, November 4, 2013

Remove all images on a website

Such a simple thing, yet Google gives you nothing on how to achieve this. So, I'll make a post about it.

Here's how you remove all images on a website:

javascript: var images = document.getElementsByTagName('img'); for(var i in images){images[i].style.display = "none"}

Create a new bookmark and put the above code in the URL field (example: Chrome)

And add the bookmark to your browser bar (example: Chrome)

Press the button and all/most pictures should disappear. (Some pictures won't, because they are coded on the website in such a way that prevents this easily. I'll try to improve the code to check for this at some point)

Saturday, January 12, 2013

What is Git and Github?

What is Git?

Per definition, Git is a distributed revision control [...] with an emphasis on speed [Wikipedia]. Let's break down those terms.

  • Distributed: The data is spread out in identical copies on multiple places, akin to torrent technology (peer-to-peer).
  • Revision control: Is the same as "version control" or "source control". It is the method by which you control, save and load different versions of your code and its changes over time.

Git implements the concept of branching. It's like a tree-branch, growing out from a stem.

A branch is initially a full copy of the original project (master). The branch can be updated and saved without affecting the master. When the branch is ready to be included into the original project it is then merged with the master - putting the code of the branch into the master.

Git is usually hosted online at a repository, such as Github. But it can also be locally on your computer. If you use an online repository, the online project and your local project will be exactly the same.

Two other essential Git concepts are "pulling" and "pushing". Pulling gets the source code from a repository. Pushing puts changes of your code into the repository.

It is important to understand that there is no real difference between an online, hosted master and offline master in your local filesystem. Almost everything you can do with an online master you can do with an offline one. Do not confuse Git with Github. Git is the system used to manage your data. Github just helps you with hosting this data online.

Why would I use Git?

Git will help you store and manage your data. Indeed, that is the very point of version control.

There is no real difference between using Git locally or online: if you work solo and on one computer. But putting your project online will give you increased flexibility - if you want to share the project to others or download at a new location, the new copy being exactly the same as on your computer. It is distributed, remember?

If you are multiple people working on the same project, Git will provide easy online-access to your data (if you use a host such as Github). Using branches you can implement a feature without messing with the original code and meddling with your other team-members by submitting incomplete code. Just create a branch instead.

It's also certainly worth linking your Github account in your resumé if you have some nice code hosted there. Github is a well respected site, and many people know what it is and how to use it. And those that do not will surely be impressed by all your amazing computer code mumbo-jumbo.

Installing Git

The easiest way to install Git is to head to and download an easy client-bundle for your operating system.
You can also download some specialized GUIs with added functionality

But we will use the first way, which gives you acces to the Git Bash console.

Now, if you sit on Windows the commands accepted by the Git Bash console will be different from what you are used to. It only takes unix commands which you can read more on here.

Configuring Git

So fire up your fresh Git Bash and let's get to it.

Some initial necessary configurations:
# the name shown as you commit changes
git config --global "firstname surname"
# you don't have to set a valid email ( is just fine) but it must be set. If you use Github, your email will function as your identification
git config --global "" 
Not needed, but very useful.
# pushes all branches relevant to the project
git config --global push.default "matching"
# pull changes before pushing for safety
git config --global branch.autosetuprebase always 
Navigate to your project and create git files necessary to manage your project (the file structure here is for Windows)
# navigate
cd C:\MyProject
# create git files
git init
You can now index the files - telling Git what files you want to manage.
# this will add all your files to Git. The . is a shorthand for "all"
git add .
If you are happy with your code, you can your files under local git management
git commit -m "My first commit"
Now you are done with local settings, and you can stop here unless you want to put your code online.

Dealing with Github

Disclaimer: The way we have approached Git so far is project-first. We assume you have some existing files you want to upload. There are other ways to do it, but we won't do that now.

Head over to Github and create your account.
Then press this icon beside your user to create a repository:

Setup a repository for your project:

You will see some text. Copy this (specific to you). Be sure to select HTTP - it's a much simpler but less secure version, but just fine for our purposes.

Now you can add a shorthand for your online git repository, so you don't have to type the URL every time (this might as well be a path on your local filesystem) Press Insert to paste into the console.
git remote add MyShortHand
Now push the changes to your repository.
git push MyShortHand
Write the password for your Github account (which must be identical to the email you configured in the beginning, and also present on your account emails) - and you're done!

Other useful commands

If anyone makes changes, you can pull them. This will update your files to be the same as the master.
git pull MyShortHand
If you have made changes to your indexed files you must use the -a flag while submitting files to git management.
git commit -a -m "I've done some changes"
If you want to create a copy of the master in another folder, just use this.
git clone MyShortHand

Alternatives to Github

The main drawback with Github is that if you want to hosting for free, your project must be public for everyone to see and download.

I personally use Bitbucket as it has no such limitations, giving you the choice to make your project public or private at no cost.

For more specific needs (user amount, project size, project amount etc) you can check out these:

Useful resources

Git tutorial by Lars Vogel:
Official online and free training for Git:
An exhausitive and free book for Git:

Friday, December 14, 2012

[C#] Read and write process memory in Windows

Disclaimer: I take no moral standpoint or legal responsibility of what you do with this information, just be aware of the consequences of your actions before you commit to them. (Tip: read license agreements)

Here is the code for this post:

Today we will manipulate a simple application, Notepad, through memory reading, writing and C# code.

Process in the System.Diagnostics namespace holds the GetProcessByName method, which returns a list of all processes with that name.
Process[] p = Process.GetProcessesByName("notepad");
We must now define a method which interacts with the Windows native libraries to read data from a process. Available through the System.Runtime.InteropServices lies OpenProcess in Kernel32, which gives us a handle we need later to both read and write in the memory allocated to the process.
public static extern int OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
We must set our desired level of Access Rights for our handle in the dwDesiredAccess parameter. This is not entirely simple. In essence, we create a flag depending on what we want to do, combining all the parameters into one. The following will give us full access.
uint DELETE = 0x00010000;
uint READ_CONTROL = 0x00020000;
uint WRITE_DAC = 0x00040000;
uint WRITE_OWNER = 0x00080000;
uint SYNCHRONIZE = 0x00100000;
uint END = 0xFFF; //if you have Windows XP or Windows Server 2003 you must change this to 0xFFFF
Let's assume you only have one Notepad process for simplicity. Since we have the Notepad process, get can get it's ID or dwProcessId. You can now get the handle like this.
int processHandle = OpenProcess(PROCESS_ALL_ACCESS, false, p[0].Id); 
The handle is required to call the two methods we use for memory reading and writing within Kernel32. Define them as the following:
public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] buffer, int size, int lpNumberOfBytesRead);

public static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] buffer, int size, int lpNumberOfBytesWritten);
To simplify things:
public byte[] ReadMemory(int adress, int processSize, int processHandle) {
    byte[] buffer = new byte[processSize];
    ReadProcessMemory(processHandle, adress, buffer, processSize, 0);
    return buffer;

public void WriteMemory(int adress, byte[] processBytes, int processHandle) {
    WriteProcessMemory(processHandle, adress, processBytes, processBytes.Length, 0);
Let's try reading first. We have the processHandle parameter, but not the adress or size of the allocated memory we are going to read from the adress position. You can get these by a number of simple or advanced methods depending on what process you want to look at. The simplest way is to use Cheat Engine.

Open notepad and find the memory location with Cheat Engine (be sure to set it to Unicode).

On my computer the string "Hello!" has been located on (0x)0026D3F0. But how big is it in memory? This is not simple in most cases. For this case, we can send the string "Hello!" to memory and read how big it is though with the following:
public int GetObjectSize(object TestObject) {
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    byte[] Array;
    bf.Serialize(ms, TestObject);
    Array = ms.ToArray();
    return Array.Length;

int processSize = GetObjectSize("Hello!");
Now we can finally read from the Notepad process!
Console.WriteLine((Encoding.Unicode.GetString(ReadMemory(0x0026D3F0, processSize, processHandle))));

Writing is easy too.
WriteMemory(0x0026D3F0, Encoding.Unicode.GetBytes("Wow!" + "\0"), processHandle);

The reason we get a trailing "!" is because it still lingers in memory. We did not overwrite the entire allocated memory for "Hello!" with our "Wow!" string.

Wednesday, December 12, 2012

[C#] Polymorphism: Exploring upcast references retaining data from child upon downcast

class Parent {}

class Child1: Parent {
    public int childInt;

class Child2: Parent {}

class Child3: Parent {
    public int childInt;

static void Main(string[] args) {
    var p = new Parent();
    var c1 = new Child1();

    c1.childInt = 5;
    p = c1; //Upcast
    c1 = (Child1) p; //Downcast
    //p.childInt not possible
    Console.WriteLine(c1.childInt); //5       

    var c2 = new Child2();
    //c2 = (Child2)p; this is legal but fails at runtime

    var c3 = new Child3();
    //c3 = (Child3)p; this is legal but fails at runtime

    c3 = p as Child3;
    if(c3 == null) //true
It would seem logical if the Parent instance lost the data from the Child upon conversion, but there is actually no conversion going on. It's only a reference.
Since we deal with a reference, p simply points to c1 - but p is not exposing the childInt field.

Now, c1 = p forces a cast since we may have other child-classes, say Child2, and Child2 might not have the fields we require. It could potentially fail at runtime (where p = c1 will never fail because upcasts cannot).

And, as we see in the commented code, c1 will have retained it's value. Makes sense when you think about it, since it's all references, but it may not seem obvious at first.

Interesting to note is that if we cast p to an identical class to Child1 with the same fields, Child3, it also fails.

If you really wanted to do an unsafe downcast, you can use the as operator. But if the conversion fails, it will default to null. This is because as has no idea how to convert our objects.

Thursday, December 6, 2012

[C#] Generics: Testing what default(T) returns

default(T) seems to accept all types which originate from object.
It's said that it returns null for all reference types, and 0 for all value types. Let's see about that.

Reference Types
Value Types

Some code to get you started (Visual Studio):

Let's make about the simplest class possible to get the result of default(T) to a Console.WriteLine()
public class Test<T>
    private T defaultData;

    public Test()
        defaultData = default(T);
    public String GetDefaultData()
        return ((bool)object.Equals(defaultData, null) ? "null" : defaultData.ToString());
The GetDefaultData method exists because defaultData will result to null in some cases, and we want to print the result to the Console (which only takes strings / char-arrays).
As we cannot call ToString() on null because it's not a real object, we have to use object.Equal, which is a static method of the static class object. It compares two objects to see if they are equal.
(Technically null is an object, but it refers to to the location of no object, so no methods can be called upon it.)

If you are confused about the code after, it's called the "if else shorthand" or "conditional operator". You read it as the following:

condition ? condition_is_true : condition_is_false

Thus we return the string "null" if the defaultData is equal to the object null, so we can print it to the console. Otherwise, we can safely use ToString since the result is not null.

We can then test our class.
struct TestStruct {
    int x;

enum TestEnum {
    x = 1, y = 2

delegate void TestDelegate();

interface TestInterface {}

static void Main(string[] args) {

    var structT = new Test < TestStruct > ();
    var enumT = new Test < TestEnum > ();
    var longT = new Test < long > ();
    var intT = new Test < int > ();
    var boolT = new Test < bool > ();
    var stringT = new Test < String > ();
    var classT = new Test < Class1 > ();
    var dynamicT = new Test < dynamic > ();
    var objectT = new Test < object > ();
    var delegateT = new Test < TestDelegate > ();
    var interfaceT = new Test < TestInterface > ();

    Console.WriteLine("default data for the following types");
    Console.WriteLine("value types");
    Console.WriteLine("struct: " + structT.GetDefaultData());
    Console.WriteLine("enum: " + enumT.GetDefaultData());
    Console.WriteLine("long: " + longT.GetDefaultData());
    Console.WriteLine("int: " + intT.GetDefaultData());
    Console.WriteLine("bool: " + boolT.GetDefaultData());
    Console.WriteLine("reference types");
    Console.WriteLine("string: " + stringT.GetDefaultData());
    Console.WriteLine("class: " + classT.GetDefaultData());
    Console.WriteLine("dynamic: " + dynamicT.GetDefaultData());
    Console.WriteLine("object: " + objectT.GetDefaultData());
    Console.WriteLine("delegate: " + delegateT.GetDefaultData());
    Console.WriteLine("interface: " + interfaceT.GetDefaultData());


I'm not testing all the variations of int, float and decimal value types, because I'm fairly certain of that they all return 0.
And the results are:

We see now that bool, which is a value type, returns False instead of 0 (makes sense...). And struct returns something weird (Main() lies in Program.cs in the ConsoleApplication1 namespace.)

If you are dissatisfied with the results, I have bad news. It is impossible to override default(T).

Sunday, December 2, 2012

[C#] Minimizing and optimizing .js in .NET (pre-4.5) - Google Closure API

Google Closure helps you minimize and optimize multiple .JS files into one file.

You can turn this code

function hello(name) {
  alert('Hello, ' + name);
hello('New user');

Into this code

function hello(a){alert("Hello, "+a)}hello("New user");

To be noted: If you're using ASP.NET 4.5 we already have this functionality out of the box:

But I made a simple library for interacting with the Google Closure API

Source paste
Project and test


using GoogleClosureHelper;

ClosureHelper.InputFolder = "JSFolder";
ClosureHelper.OutputFile = "Compiled.js";
ClosureHelper.CompileToOutputPath("SIMPLE_OPTIMIZATIONS", "text", "compiled_code");

Look here for the parameters:

Web interface: